27
loading...
This website collects cookies to deliver better user experience
Python 3.8.2
>>> one = ["one","two", "three"]
>>> two = ["four","five"]
>>> one + two
['one', 'two', 'three', 'four', 'five']
+
list1 + list2
is the way you want to concatenate lists.+
is generally the best choice. All options covered work in Python 2.3, Python 2.7, and all versions of Python 31.>>>longlist = ["one","two", "three"] * 1000
['one', 'two', 'three', 'one', 'two', 'three', ... ]
>>>shortlist = ["four","five"]
["four","five"]
>>> x.extend(y)
>>> x
['one', 'two', 'three', 'one', ..., "four","five"]
x + y
otherwise.timeit
module to check some performance numbers.# Performance Check
>>> setup = """\
x = ["one","two","three"] * 1000
y = ["four","five","six"]
"""
# x + y with large x
>>> timeit.timeit('x + y', setup=setup, number=1000000)
3.6260274310000113
# x.extend(y) with large x
>>> timeit.timeit('x.extend(y)', setup=setup, number=1000000)
0.06857255800002804
x + y
will work just fine. This is because Python stores references to the values in the list, not the values themselves. Thus, the element size makes no difference to the runtime complexity.>>> x = ["one" * 1000, "two" * 1000, "three" * 1000]
>>> y = ["four" * 1000, "five" * 1000]
>>> #This is fine
>>> z = x + y
>>> #Performance Testing (extend is slower for large elements)
>>> setup = """\
x = ["one" * 1000, "two" * 1000, "three" * 1000]
y = ["four" * 1000, "five" * 1000]
"""
>>> timeit.timeit('x + y', setup=setup, number=1000000)
0.05397573999994165
>>> timeit.timeit('x.extend(y)', setup=setup, number=1000000)
0.06511967799997365
extend
does not have an advantage.chain
from itertools
to create an iterable of two lists.>>>longlist = ["one","two", "three"] * 1000
['one', 'two', 'three', 'one', 'two', 'three',, .......... ]
>>>shortlist = ["four","five"]
["four","five"]
>>> from itertools import chain
>>> z = list(chain(longlist, shortlist)
['one', 'two', 'three', 'one', , .........., "four","five"]
>>> setup = """\
from itertools import chain
x = ["one","two","three"] * 1000
y = ["four","five","six"]
"""
# x + y with large x
# x.extend(y) with large x
>>> timeit.timeit('x.extend(y)', setup=setup, number=1000000)
0.06857255800002804
>>> timeit.timeit('list(chain(x, y))', setup=setup, number=1000000)
16.810488051999982
chain
with two lists is slower in all cases tested, and x + y
is easier to understand.+
for concatenate works great.>>> one = ["one","two", "three"]
>>> two = ["four","five"]
>>> three = []
>>> z = one + two + three
chain
from itertools
becomes a great option. Chain takes a list of lists and flattens it into a single list.>>> l = [["one","two", "three"],["four","five"],[]] * 99
[['one', 'two', 'three'], ['four', 'five'], [], ...
>>> list(chain.from_iterable(l))
['one', 'two', 'three', 'four', 'five', 'one', 'two', ... ]
chain
can take anything iterable, making it an excellent choice for combining lists, dictionaries, and other iterable structures.>>> from itertools import chain
>>> one = [1,2,3]
>>> two = {1,2,3}
>>> list(chain(one, two, one))
[1, 2, 3, 1, 2, 3, 1, 2, 3]
result = []
for nestedlist in l:
result.extend(nestedlist)
>>> setup = """\
from itertools import chain
l = [["one","two", "three"],["four","five"],[]] * 99
"""
>>> # Add Nested Lists using chain.from_iterable
>>> timeit.timeit('list(chain.from_iterable(l))', setup=setup, number=100000)
1.0384087909997106
>>> ### Add using our own iteration
>>> run = """\
result = []
for nestedlist in l:
result.extend(nestedlist)
"""
>>> timeit.timeit(run, setup=setup, number=100000)
1.8619721710001613
chain.from_iterable
is faster than extend.extend
with N lists.chain.from_iterable
.>>> # Method 1 - chain.from_iterable
>>> longlist = ["one","two", "three"] * 1000
>>> nestedlist = [longlist, ["one","two", "three"],["four","five"],[]]
>>> list(chain.from_iterable(nestedlist))
>>> setup = """\
from itertools import chain
longlist = ["one","two", "three"] * 1000;
combinedlist = [longlist, ["one","two", "three"],["four","five"],[]]
"""
>>> timeit.timeit('list(chain.from_iterable(combinedlist))', setup=setup, number=100000)
1.8676087710009597
>>> # Method 2 - extend
>>> longlist = ["one","two", "three"] * 1000
>>> nestedlist = [["one","two", "three"],["four","five"],[]]
>>> for item in nestedlist:
>>> longlist.extent(item)
>>> setup = """\
from itertools import chain
longlist = ["one","two", "three"] * 1000;
nestedlist = [["one","two", "three"],["four","five"],[]]
"""
>>> run = """\
for item in nestedlist:
longlist.extend(item)
"""
>>> timeit.timeit(run, setup=setup, number=100000)
0.02403609199973289
extend
is much faster when flattening lists or concatenating many lists with one long list. If you encounter this, using extend
to add the smaller lists to the long list can decrease the work that has to be done and increase performance.Condition | Solution | Performance Optimization2 |
---|---|---|
2 lists | x + y |
No |
1 large list, 1 small list | x.extend(y) |
Yes |
Known number of N lists | x + y + z |
No |
Unknown number of N lists | list(chain.from_iterable(l)) |
No |
List of Lists | list(chain.from_iterable(l)) |
No |
1 large list, many small lists | for l1 in l: x.extend(...) |
Yes |