This is closely related to the answers given to list comprehension vs map as you're using a lambda
with your reduce
statement you're sending python code to be ran for each iteration thus slowing the reduce down. List comprehensions were meant to be much more efficient and readable thus they are the prefered method of choice.
That said why not use itertools.chain.from_iterable
as well as map
ping operator.itemgetter
. This results in the same output while also utilizing some great builtin methods. Haven't tested for speed
>>> from itertools import chain
>>> from operator import itemgetter
>>> arr = array([[array([33120, 28985, 9327, 45918, 30035, 17794, 40141, 1819, 43668],
dtype='int64')],
[array([33754, 24838, 17704, 21903, 17668, 46667, 17461, 32665],
dtype='int64')],
[array([46842, 26434, 39758, 27761, 10054, 21351, 22598, 34862, 40285,
17616, 25146, 32645, 41276], dtype='int64')],
[array([24534, 8230, 14267, 9352, 3543, 29397, 900, 32398, 34262,
37646, 11930, 37173], dtype='int64')],
[array([25157], dtype='int64')],
[array([ 8859, 20850, 19322, 8075], dtype='int64')]], dtype=object)
>>> array(list(chain.from_iterable(map(itemgetter(0),arr.tolist()))))
[33120 28985 9327 45918 30035 17794 40141 1819 43668 33754 24838 17704
21903 17668 46667 17461 32665 46842 26434 39758 27761 10054 21351 22598
34862 40285 17616 25146 32645 41276 24534 8230 14267 9352 3543 29397
900 32398 34262 37646 11930 37173 25157 8859 20850 19322 8075]