Social Media

1/02/2019

Taking list from user

There can be many ways to take the list from the user.]
1. Taking element one by one and append it in  the list
l=list()
n=int(input("enter number of elements : "))
for i in range(n):
    x=input()
    l.append(x)
print(l)

2. Using map() function:
l=list(map(int,input().split()))
This function converts the value entered by the user and append it into the list one by one.
split() function tells us where inputs end. By default it is space. After space character next input is counted.

3. Using for loop in single line:
l=[int(x) for x in input().split()]

4. Using eval() function:
l=eval(input("Enter the list"))   #[1,2,3]
print(l)
   


Be First to Post Comment !
Post a Comment