So there are two ways to take a list and add the members of a second list to the first. You can use list concatenation or your can iterate over it. You can:
for obj in list2:
list1.append(obj)
or you can:
list1 = list1 + list2
or
list1 += list2
My question is: which is faster, and why? I tested this using two extremely large lists (upwards of 10000 objects) and it seemed the iterating method was a lot faster than the list concatenation (as in l1 = l1 + l2). Why is this? Can someone explain?
You're measuring wrong; iterating and calling
append
multiple times is way slower than doing it one call since the overhead of the many function call (at least in cpython) dwarfs anything that has to do with the actual list operation, as shown here with cPython 2.7.5 on Linux x64:Note that
x = x + y
creates a second copy of the list (at least in cPython).x.extend(y)
and its cousinx += y
do the same thing as callingappend
multiple times, just without the overhead of actually calling a Python method.append
adds each item one at a time, which is the cause of its slowness, as well as the repeated function calls toappend
.However in this case the
+=
operator is not syntactic sugar for the+
. The+=
operator does not actually create a new list then assign it back, it modifies the left hand operand in place. It's pretty apparent when usingtimeit
to use both 10,000 times.+=
is much faster (about 500x)You also have the
extend
method for lists which can append any iterable (not just another list) with something likel.extend(l2)
Logically equivalent to appending, but much much faster as you can see.
So to explain this: iterating is faster than
+
because+
has to construct an entire new listextend
is faster than iteration because it's a builtin list method and has been optimized. Logically equivalent to appending repeatedly, but implemented differently.+=
is faster thanextend
because it can modify the list in place, knowing how much larger the list has to be and without repeated function calls. It assumes you're appending your list with another list/tupleI ran the following code
and got this, which says that adding lists (+=) is faster.
0.016047026962041855
0.0019438499584794044