Which is the most pythonic way and the fastest way (could be the same) to append many list together? For example, given the lists below:
a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]
we get one list:
combined = [1, 2, 3, 4, 5, 6, 7, 8]
Which is the most pythonic way and the fastest way (could be the same) to append many list together? For example, given the lists below:
a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]
we get one list:
combined = [1, 2, 3, 4, 5, 6, 7, 8]
In Python 3.5+, you can use the generic unpacking:
combined = [*a, *b, *c, *d]
or prior to Python 3.5+, you can use itertools.chain
:
from itertools import chain
combined = list(chain(a, b, c, d))
I can't understand you do you mean how to merge them? for example
a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]
combined = a + b + c + d
so combined will be
[1, 2, 3, 4, 5, 6, 7, 8]
OR:
a = [1, 2]
b = [3, 4]
c = [5, 6]
d = [7, 8]
a.extend(b)
a.extend(c)
a.extend(d)
Now:
print(a)
Returns:
[1, 2, 3, 4, 5, 6, 7, 8]