I would like to know how to convert a string like "1 -2 -8 4 5"
into something like [1, -2, -8, 4, 5]
Asked
Active
Viewed 116 times
-1

jonrsharpe
- 115,751
- 26
- 228
- 437

Pablo Clsn
- 13
- 1
- 6
-
```I would like to know how to ...``` - Work your way through [The Tutorial](https://docs.python.org/3/tutorial/index.html) and you will start getting ideas how to solve the problem. Look through [the docs](https://docs.python.org/3/library/index.html) to get some more ideas. Try some of your ideas out. – wwii Mar 04 '17 at 16:02
3 Answers
3
I'd split the string by the " "
character, and convert each element to an int
:
myList = [int(x) for x in myString.split(" ")]
or simply
myList = [int(x) for x in myString.split()]
by default split uses the white space ' '
as argument

Mureinik
- 297,002
- 52
- 306
- 350