68

I have a string "42 0" (for example) and need to get an array of the two integers. Can I do a .split on a space?


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? .

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Jonathan
  • 723
  • 1
  • 5
  • 6
  • 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

9 Answers9

145

Use str.split():

>>> "42 0".split()  # or .split(" ")
['42', '0']

Note that str.split(" ") is identical in this case, but would behave differently if there were more than one space in a row. As well, .split() splits on all whitespace, not just spaces.

Using map usually looks cleaner than using list comprehensions when you want to convert the items of iterables to built-ins like int, float, str, etc. In Python 2:

>>> map(int, "42 0".split())
[42, 0]

In Python 3, map will return a lazy object. You can get it into a list with list():

>>> map(int, "42 0".split())
<map object at 0x7f92e07f8940>
>>> list(map(int, "42 0".split()))
[42, 0]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Lauritz V. Thaulow
  • 49,139
  • 12
  • 73
  • 92
66
text = "42 0"
nums = [int(n) for n in text.split()]
kindall
  • 178,883
  • 35
  • 278
  • 309
13
l = (int(x) for x in s.split())

If you are sure there are always two integers you could also do:

a,b = (int(x) for x in s.split())

or if you plan on modifying the array after

l = [int(x) for x in s.split()]
GWW
  • 43,129
  • 11
  • 115
  • 108
8

This should work:

[ int(x) for x in "40 1".split(" ") ]
Bruno
  • 119,590
  • 31
  • 270
  • 376
5

Of course you can call split, but it will return strings, not integers. Do

>>> x, y = "42 0".split()
>>> [int(x), int(y)]
[42, 0]

or

[int(x) for x in "42 0".split()]
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
3

You can split and ensure the substring is a digit in a single line:

In [1]: [int(i) for i in '1 2 3a'.split() if i.isdigit()]
Out[1]: [1, 2]
Dos
  • 2,250
  • 1
  • 29
  • 39
3

Other answers already show that you can use split() to get the values into a list. If you were asking about Python's arrays, here is one solution:

import array
s = '42 0'
a = array.array('i')
for n in s.split():
    a.append(int(n))

Edit: A more concise solution:

import array
s = '42 0'
a = array.array('i', (int(t) for t in s.split()))
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
0

Given: text = "42 0"

import re
numlist = re.findall('\d+',text)

print(numlist)

['42', '0']
Blue
  • 22,608
  • 7
  • 62
  • 92
0

Use numpy's fromstring:

import numpy as np

np.fromstring("42 0", dtype=int, sep=' ')
>>> array([42,  0])
iacob
  • 20,084
  • 6
  • 92
  • 119