Possible Duplicate:
In Python, what is the difference between “.append()” and “+= []”?
In Python, I’ve recently noticed that you can append list items in two ways:
a.append(1)
a += [1]
I like using the second approach because it is more readable for me. Are there any downsides to using it?
Those two methods aren’t quite equivalent. The
+=method:requires that you first create a new list containing the single element
1, tack it on to the lista, then discard the single-element list. This would be more equivalent to:You will likely find that
a.append(1)does less work, since it does not need to create a single-element list which it’s just going to throw away in the next step.