HOW TO TAKE LIST AS INPUT IN PYTHON

This article will show you how to take list as input in python. If you are looking for “How to input list in Python”, this article will help for the same. 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("Output: ",li)
Output:
Enter number of elements: 4 1 2 3 4 Output: ['1', '2', '3', '4']
Method 2 : Using map()
n=int(input("Enter number of elements: ")) a = list(map(int,input("Enter the numbers : ").strip().split(',')))[:n] print (a)
Output:
Enter number of elements: 5 Enter the numbers : 2,3,4,1,3 [2, 3, 4, 1, 3]
Subscribe
Login
Please login to comment
0 Discussion