Get a list of numbers as input from the user
Solution 1
In Python 3.x, use this.
a = [int(x) for x in input().split()]
Example
>>> a = [int(x) for x in input().split()]
3 4 5
>>> a
[3, 4, 5]
>>>
Solution 2
It is much easier to parse a list of numbers separated by spaces rather than trying to parse Python syntax:
Python 3:
s = input()
numbers = list(map(int, s.split()))
Python 2:
s = raw_input()
numbers = map(int, s.split())
Solution 3
eval(a_string)
evaluates a string as Python code. Obviously this is not particularly safe. You can get safer (more restricted) evaluation by using the literal_eval
function from the ast
module.
raw_input()
is called that in Python 2.x because it gets raw, not "interpreted" input. input()
interprets the input, i.e. is equivalent to eval(raw_input())
.
In Python 3.x, input()
does what raw_input()
used to do, and you must evaluate the contents manually if that's what you want (i.e. eval(input())
).
Solution 4
You can use .split()
numbers = raw_input().split(",")
print len(numbers)
This will still give you strings, but it will be a list of strings.
If you need to map them to a type, use list comprehension:
numbers = [int(n, 10) for n in raw_input().split(",")]
print len(numbers)
If you want to be able to enter in any Python type and have it mapped automatically and you trust your users IMPLICITLY then you can use eval
Solution 5
Another way could be to use the for-loop for this one. Let's say you want user to input 10 numbers into a list named "memo"
memo=[]
for i in range (10):
x=int(input("enter no. \n"))
memo.insert(i,x)
i+=1
print(memo)
Comments
-
Underyx almost 2 years
I tried to use
input
(Py3) /raw_input()
(Py2) to get a list of numbers, however with the codenumbers = input() print(len(numbers))
the input
[1,2,3]
and1 2 3
gives a result of7
and5
respectively – it seems to interpret the input as if it were a string. Is there any direct way to make a list out of it? Maybe I could usere.findall
to extract the integers, but if possible, I would prefer to use a more Pythonic solution. -
AJ Dhaliwal about 8 yearsAfter python 2.7 raw_input() was renamed to input(). Stack overflow answer
-
Underyx almost 8 yearsI don't think this is simpler than the accepted answer.
-
greentec about 7 yearsintrotopython.org/lists_tuples.html#List-Comprehensions maybe this is helpful.
-
frp farhan about 7 yearswhat if my input is having mixed datatype string and integer then how can i split them and convert to list. input: 'aaa' 3 45 554 'bbb' 34 'ccc' i added the contents seperated by space!!
-
Sven Marnach about 7 years@FarhanPatel That's an unrelated question, so I suggest asking a new question. Start with looping over
s.split()
orshlex.split(s)
if you want to allow spaces inside quoted strings. -
frp farhan about 7 yearsposting it as a new question, thnx for the encouragement, was scary it might not be marked as duplicate!! stackoverflow.com/questions/43822895/…
-
Stevoisiak over 6 yearsCan this accept different data types other than int? i.e. Can I replace
int(x)
withstring(x)
-
Vikhyat Agarwal over 6 years@StevenVascellaro Yes although the expression would be str(x) instead of string(x) since str is python's way of saying string
-
Bernhard Barker about 6 years@DhirajBarnwal If you want to know what's going on there:
input()
to read input +split()
to split the input on spaces +[f(x) for x in iter]
to loop over each of those +int
to turn each into an int. If you want to know how one can come up with it: it's mostly just figuring out what you want to do and which pieces you need to put together to achieve that. -
Rishav about 5 yearsI would have +1 if you had an extra 2 lines of explanation which @Dukeling did in the comments.
-
Tomerikoo about 3 yearsThis takes each number as a separate input, on a separate line. The question implies a single line of input with one call to
input
... -
Tomerikoo about 3 yearsWhat if I want to pass
1, 11, 111
or maybe11, 1, 111
? In your program they will all just be1, 1, 1, 1, 1, 1
... Bottom line, this only works for single digit numbers which is pretty restrictive...