How to convert a string of space- and comma- separated numbers into a list of int?

153,905

Solution 1

Split on commas, then map to integers:

map(int, example_string.split(','))

Or use a list comprehension:

[int(s) for s in example_string.split(',')]

The latter works better if you want a list result, or you can wrap the map() call in list().

This works because int() tolerates whitespace:

>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
>>> list(map(int, example_string.split(',')))  # Python 3, in Python 2 the list() call is redundant
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
>>> [int(s) for s in example_string.split(',')]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]

Splitting on just a comma also is more tolerant of variable input; it doesn't matter if 0, 1 or 10 spaces are used between values.

Solution 2

I guess the dirtiest solution is this:

list(eval('0, 0, 0, 11, 0, 0, 0, 11'))

Solution 3

number_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'

number_string = number_string.split(',')

number_string = [int(i) for i in number_string]

Solution 4

it should work

example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
example_list = [int(k) for k in example_string.split(',')]

Solution 5

You can also use list comprehension on splitted string

[ int(x) for x in example_string.split(',') ]
Share:
153,905

Related videos on Youtube

Bart M
Author by

Bart M

Python n00b

Updated on August 28, 2021

Comments

  • Bart M
    Bart M over 2 years

    I have a string of numbers, something like:

    example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
    

    I would like to convert this into a list:

    example_list = [0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
    

    I tried something like:

    for i in example_string:
        example_list.append(int(example_string[i]))
    

    But this obviously does not work, as the string contains spaces and commas. However, removing them is not an option, as numbers like '19' would be converted to 1 and 9. Could you please help me with this?

    • Jochen Ritzel
      Jochen Ritzel over 10 years
      example_string.split(',')
  • Patrice
    Patrice over 9 years
    Hey Rohit :). Welcome to SO. Do you mind adding a bit explanations to your code? So the next guys coming in have a better idea of what you're doing with your answer :).
  • Marcos
    Marcos over 7 years
    If the original string of numbers was stored in "python format", this solution will actually adapt to whatever type of data is stored. '0, 0, 0, 11' will map to int type and '0.12, 0.1, 1.2' will map to float type without having to explicitly code for it.
  • Aran-Fey
    Aran-Fey almost 6 years
    It could be dirtier, but it's dirty enough to earn you my downvote. Why even post a horrible solution like that?
  • Tengerye
    Tengerye over 5 years
    For the python3, you have to change map(int, example_string.split(',')) to list(map(int, example_string.split(','))).
  • Martijn Pieters
    Martijn Pieters over 5 years
    @Tengerye: that depends; if you only need an iterable the map() object is fine as it is.
  • Emjey
    Emjey over 4 years
    Thanks a million!!!!