n=int(input("enter the number of elements of your list: ")) #input data from the user and convert it to integer type numbers=[] #create an empty list for i in range(n): #iterate my list from index 0 to n-1 and repeat the block of instructions below for each i x = int(input(f"enter the elements for numbers {[i]}: ")) #store the elements of the list from user in x variable numbers.append(x) #add each element at the end of the list numbers print(numbers) #output the new list
This can also be achieved using below code for storing items as string not int. items = input("Enter multiple items: ") list = items.split() print(list)
@Neso academy Great video , it helps me a lot as usual but what about if we want a list which is a collection of different types of items ? because with the example in the course all the items of our list must be an integer
The input function always returns a value as a string. If we are to obtain numbers then we'll have to convert the type of the obtained item to integer (int) or float. we can also do, numbers = [ ] for i in range (3): x = input() y = int(x) numbers.append(y) The method in the video is for getting numbers as input and type is being converted in the same line in which input is being taken.
Thanks for the vid. Just a suggestion… what if you need to input hundreds of numbers? The user is not going to want take a count of each number and possibly miscount. Might be better just to input numbers and type END when finished.