-1

I have a string of integers for ex. 1 2 3 4 5. How do I convert it into a list like [1, 2, 3, 4, 5]?

CoolBoiBS
  • 23
  • 5
  • 6
    What exactly do you mean by "something like 1 2 3 4 5"? – mkrieger1 Sep 18 '21 at 20:56
  • 1
    If you have a "list of integers" you already have a list. – Samwise Sep 18 '21 at 21:00
  • By that I was asking how to convert a "list" of numbers which in this example was 1 2 3 4 5 into an actual list [1, 2, 3, 4, 5]. Sorry if that wasn't clear! – CoolBoiBS Sep 18 '21 at 21:02
  • Either way, Sabil answered with what I was looking for. – CoolBoiBS Sep 18 '21 at 21:03
  • A more clear way to describe what you have would be 'the string `"1 2 3 4 5"`'. There are a lot of things that are "like 1 2 3 4 5" (maybe you mean the tuple `(1, 2, 3, 4, 5)`?) but if you say you have a *string* it's very clear which one of them you're talking about! – Samwise Sep 18 '21 at 21:10

1 Answers1

1

You can do this:

int_str = '1 2 3 4 5'

int_list = list(map(int, int_str.split()))

print(int_list)

Output:

[1, 2, 3, 4, 5]
Sabil
  • 3,750
  • 1
  • 5
  • 16