51

I have a string of numbers like so:

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 it to 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 this code:

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

Obviously it does not work, as the string contains spaces and commas. However, even if I remove those, the individual digits of the numbers get converted, giving a result like [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 9, 0, 9, 0, 0, 0, 0, 0, 0, 1, 1].

How can I get the desired result?


See also: How to split a string of space separated numbers into integers? . This question adds a small additional consideration: since int can accept leading and trailing whitespace, there is reason to split on just , rather than , .

The obvious approach to this problem is a common combination of simple tasks:

However, depending on the exact requirements, it can be addressed in other ways. For some cases, the split step may be sufficient on its own. There are other approaches, given in some answers here, to produce different output besides a native Python list - for example, using the standard library array.array, or a NumPy array.

Alternately, the problem can be seen as a special case of How to extract numbers from a string in Python? .

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bart M
  • 697
  • 1
  • 6
  • 18
  • Somewhat related: [How to split by comma and strip white spaces in Python?](https://stackoverflow.com/questions/4071396) – Karl Knechtel May 18 '23 at 22:44

6 Answers6

98

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
14

I guess the dirtiest solution is this:

list(eval('0, 0, 0, 11, 0, 0, 0, 11'))
hendrik
  • 1,902
  • 16
  • 14
  • 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. – Marcos Nov 10 '16 at 07:56
  • 3
    It could be dirtier, but it's dirty enough to earn you my downvote. Why even post a horrible solution like that? – Aran-Fey Jun 03 '18 at 07:09
7

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(',')]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lucemia
  • 6,349
  • 5
  • 42
  • 75
7
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]
Edd
  • 3,724
  • 3
  • 26
  • 33
Rohit Singh
  • 2,143
  • 3
  • 13
  • 16
  • 1
    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 :). – Patrice Oct 08 '14 at 15:17
6

You can also use a list comprehension on split string:

[ int(x) for x in example_string.split(',') ]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lejlot
  • 64,777
  • 8
  • 131
  • 164
2

Try this:

import re
[int(s) for s in re.split('[\s,]+',example_string)]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131