How to input a list in python

In this article, we are going to explore how we can input a list in python. A list is a collection that is ordered and changeable. In python, a list is created by placing the elements inside square brackets[], separated by commas.
Method 1
n=int(input("Enter number of elements: ")) li=[] #creating an empty list for i in range(n): element=input() li.append(element) #appending the elements to the list print(li)
Method 2 : Using map()
n=int(input("Enter number of elements: ")) a = list(map(int,input("Enter the numbers : ").strip().split(',')))[:n] print (a)
Subscribe
Login
Please login to comment
0 Discussion