It depends on the Python implementation, but append
will never be slower than the second variant.
a += [1]
creates a temporary list with one element. Additionally, the +=
operator has to perform some extra work to determine the size of the new list. A good Python implementation may reduce the overhead by not actually constructing the list [1]
in memory.
As with virtually every performance question, it doesn't matter unless your code is really performance-critical. With cpython 2.7, I measured the following values:
>>> import timeit
>>> timeit.timeit('l = []\nfor i in range(200):\n\tl.append(1)\n')
27.95561385154724
>>> timeit.timeit('l = []\nfor i in range(200):\n\tl += [1]\n')
37.52841401100159