Here are some examples and brief explanation for Inputting Lists from users:
You may often need code that reads data from the console into a list. You can enter one data
item per line and append it to a list in a loop. For example, the following code reads ten numbers one per line into a list.
lst1 = [] # Create an empty list
print("Enter 10 Numbers: ")
for i in range(10):
lst1.append(eval(input()))
print(lst1)
Sometimes it is more convenient to enter the data in one line separated by spaces. You can
use the string’s split method to extract data from a line of input. For example, the following
code reads ten numbers separated by spaces from one line into a list.
# Read numbers as a string from the console
s = input("Enter 10 numbers separated by spaces from one line: ")
items = s.split() # Extract items from the string
lst2 = [eval(x) for x in items] # Convert items to numbers
print(lst2)
Invoking input()
reads a string. Using s.split()
extracts the items delimited by
spaces from string s
and returns items in a list. The last line creates a list of numbers by converting the items into numbers.