Change a string of integers separated by spaces to a list of int
The most simple solution is to use .split()
to create a list of strings:
x = x.split()
Alternatively, you can use a list comprehension in combination with the .split() method:
x = [int(i) for i in x.split()]
You could even use map map
as a third option:
x = list(map(int, x.split()))
This will create a list
of int
's if you want integers.
No need to worry, because python provide split() function to change string into a list.
x='1 2 3 4 67 8 9'
x.split()
['1', '2', '3', '4', '67', '8']
or if you want output in integer form then you can use map function
map(int ,x.split(' '))
[1, 2, 3, 4, 67, 8]