input that shows user values of a list that user gives input for in python

In Python, one way to take input from a user for a list and then display the values of the list using a for loop is as follows:

main.py
# Taking input from user for a list
user_input = input("Enter values of list separated by space: ")
user_list = user_input.split()

# Displaying the values of the list using a for loop and print statement
print("Values of the list are:")
for val in user_list:
    print(val)
275 chars
9 lines

In the above code, we are taking input from the user for the values of the list using the input() function. We are then splitting the input string using the split() method which returns a list of strings.

After that, we are using a for loop to iterate over each value of the list and then printing it using the print() statement.

Note: Since the input() function returns a string, we are using the split() method to convert the string into a list of strings. If the user inputs integers or floats separated by space, we need to further convert them into the respective data types.

gistlibby LogSnag