How to input a list in python
![How to input a list in Python](https://pythonpoint.net/wp-content/uploads/2020/11/How-to-convert-string-to-int-in-python-4-1024x576.jpg)
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