-1

I have a string "12 21 3 9 21 19" and I would like to split it up into a list [12, 21, 3, 9, 21, 19].

Doing newlist = list("12 21 3 9 21 19") breaks it up to [1,2,2,1,3,9,2,1,1,9]! Any help would be greatly appreciated :)

Lucius Young
  • 3
  • 1
  • 3

1 Answers1

1

You can use .split()

e.g. -

>>> my_string = "12 21 3 9 21 19"
>>> a_new_list = my_string.split()
>>> print(a_new_list)
>>> ["12", "21", "3", "9", "21", "19"]

If you wanted the actual members of the list to be ints you could convert them to integers using map or some other method.

Pythonista
  • 11,377
  • 2
  • 31
  • 50