Add all elements of an iterable to list
Use list.extend()
, not list.append()
to add all items from an iterable to a list:
l.extend(t)
l.extend(t2)
or
l.extend(t + t2)
or even:
l += t + t2
where list.__iadd__
(in-place add) is implemented as list.extend()
under the hood.
Demo:
>>> l = []
>>> t = (1,2,3)
>>> t2 = (4,5)
>>> l += t + t2
>>> l
[1, 2, 3, 4, 5]
If, however, you just wanted to create a list of t + t2
, then list(t + t2)
would be the shortest path to get there.
stuff.extend
is what you want.
t = [1,2,3]
t2 = [4,5]
t.extend(t2)
# [1, 2, 3, 4, 5]
Or you can do
t += t2