I want to insert 5 integers by simply typing 3 4 6 8 9
and hitting enter. I know how to insert strings in a list by using list=raw_input().split(" ",99)
, but how can I insert integers using space?

- 2,209
- 8
- 24
- 31

- 61
- 1
- 6
4 Answers
>>> integers_list = [int(i) for i in raw_input().split()]
>>> integers_list
[22, 33, 11]
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
Also, here you can read about difference between a map and list comprehension.

- 1
- 1

- 1,467
- 1
- 18
- 34
-
1You do not convert to integer – mikeb Jan 11 '16 at 16:30
map(int, "1 2 3 4 5".split())
This will take your string and convert to a list of ints.
Split defaults to splitting on a space, so you don't need an argument.
For raw_input(), you can do:
map(int, raw_input().split())

- 10,578
- 7
- 62
- 120
In [1]: my_list = map( int, raw_input().split() )
1 2 3 4 5
In [2]: my_list
Out[2]: [1, 2, 3, 4, 5]

- 19,045
- 18
- 72
- 99
The above answer is perfect if you are looking to parse strings into a list. Else you can parse them into Integer List using the given way
integers = '22 33 11' integers_list = []
try: integers_list = [int(i) for i in integers.split(' ')]
except: print "Error Parsing Integer"
print integers_list

- 33
- 3