how to add all array's elements to one list in python
Question:
with a 2 dimension array which looks like this one:
myarray = [['jacob','mary'],['jack','white'],['fantasy','clothes'],['heat','abc'],['edf','fgc']]
every elements is an array which has fixed length elements. how to become this one,
mylist = ['jacob','mary','jack','white','fantasy','clothes','heat','abc','edf','fgc']
here’s my solve
mylist = []
for x in myarray:
mylist.extend(x)
should be more simple i guess
Answers:
>>> myarray = [['jacob','mary'],['jack','white'],['fantasy','clothes'],['heat','abc'],['edf','fgc']]
>>> sum(myarray,[])
['jacob', 'mary', 'jack', 'white', 'fantasy', 'clothes', 'heat', 'abc', 'edf', 'fgc']
Or
>>> [i for j in myarray for i in j]
['jacob', 'mary', 'jack', 'white', 'fantasy', 'clothes', 'heat', 'abc', 'edf', 'fgc']
Use itertools.chain.from_iterable
:
from itertools import chain
mylist = list(chain.from_iterable(myarray))
Demo:
>>> from itertools import chain
>>> myarray = [['jacob','mary'],['jack','white'],['fantasy','clothes'],['heat','abc'],['edf','fgc']]
>>> list(chain.from_iterable(myarray))
['jacob', 'mary', 'jack', 'white', 'fantasy', 'clothes', 'heat', 'abc', 'edf', 'fgc']
However, Haidro’s sum()
solution is faster for your shorter sample:
>>> timeit.timeit('f()', 'from __main__ import withchain as f')
2.858742465992691
>>> timeit.timeit('f()', 'from __main__ import withsum as f')
1.6423718839942012
>>> timeit.timeit('f()', 'from __main__ import withlistcomp as f')
2.0854451240156777
but itertools.chain
wins if the input gets larger:
>>> myarray *= 100
>>> timeit.timeit('f()', 'from __main__ import withchain as f', number=25000)
1.6583486960153095
>>> timeit.timeit('f()', 'from __main__ import withsum as f', number=25000)
23.100156371016055
>>> timeit.timeit('f()', 'from __main__ import withlistcomp as f', number=25000)
2.093297885992797
with a 2 dimension array which looks like this one:
myarray = [['jacob','mary'],['jack','white'],['fantasy','clothes'],['heat','abc'],['edf','fgc']]
every elements is an array which has fixed length elements. how to become this one,
mylist = ['jacob','mary','jack','white','fantasy','clothes','heat','abc','edf','fgc']
here’s my solve
mylist = []
for x in myarray:
mylist.extend(x)
should be more simple i guess
>>> myarray = [['jacob','mary'],['jack','white'],['fantasy','clothes'],['heat','abc'],['edf','fgc']]
>>> sum(myarray,[])
['jacob', 'mary', 'jack', 'white', 'fantasy', 'clothes', 'heat', 'abc', 'edf', 'fgc']
Or
>>> [i for j in myarray for i in j]
['jacob', 'mary', 'jack', 'white', 'fantasy', 'clothes', 'heat', 'abc', 'edf', 'fgc']
Use itertools.chain.from_iterable
:
from itertools import chain
mylist = list(chain.from_iterable(myarray))
Demo:
>>> from itertools import chain
>>> myarray = [['jacob','mary'],['jack','white'],['fantasy','clothes'],['heat','abc'],['edf','fgc']]
>>> list(chain.from_iterable(myarray))
['jacob', 'mary', 'jack', 'white', 'fantasy', 'clothes', 'heat', 'abc', 'edf', 'fgc']
However, Haidro’s sum()
solution is faster for your shorter sample:
>>> timeit.timeit('f()', 'from __main__ import withchain as f')
2.858742465992691
>>> timeit.timeit('f()', 'from __main__ import withsum as f')
1.6423718839942012
>>> timeit.timeit('f()', 'from __main__ import withlistcomp as f')
2.0854451240156777
but itertools.chain
wins if the input gets larger:
>>> myarray *= 100
>>> timeit.timeit('f()', 'from __main__ import withchain as f', number=25000)
1.6583486960153095
>>> timeit.timeit('f()', 'from __main__ import withsum as f', number=25000)
23.100156371016055
>>> timeit.timeit('f()', 'from __main__ import withlistcomp as f', number=25000)
2.093297885992797