the map should be slightly faster than the comp list:
import numpy as np arr = np.array(map(int,'00101'))
Some timings show that it is in a string of 1024 characters:
In [12]: timeit np.array([int(c) for c in s]) 1000 loops, best of 3: 422 µs per loop In [13]: timeit np.array(map(int,s)) 1000 loops, best of 3: 389 µs per loop
Just a call list in s and using dtype = int is faster:
In [20]: timeit np.array(list(s), dtype=int) 1000 loops, best of 3: 329 µs per loop
Using fromiter and passing dtype=int is faster:
In [21]: timeit np.fromiter(s,dtype=int) 1000 loops, best of 3: 289 µs per loop
Borrowing an answer from this using fromstring and uint8, since dtype is the fastest:
In [54]: timeit np.fromstring(s, 'int8') - 48 100000 loops, best of 3: 4.54 µs per loop
Even re-creating the name and changing the dtype is still much faster:
In [71]: %%timeit ....: arr = np.fromstring(s, 'int8') - 48 ....: arr = arr.astype(int) ....: 100000 loops, best of 3: 6.23 µs per loop
Even significantly faster than Ashwini joins:
In [76]: timeit np.fromstring(' '.join(s), sep=' ', dtype=int) 10000 loops, best of 3: 62.6 µs per loop
As @Unutbu commented, np.fromstring(s, 'int8') - 48 not limited to ones and zeros, but will work for all strings consisting of ASCII digits.