What is the difference between:
some_list1 = [] some_list1.append("something")
and
some_list2 = [] some_list2 += ["something"]
In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.
Technically speaking, one is a function that returns an object casted to a list, and the other is the literal list object itself. Kinda like int(0) vs 0 . In practical terms there's no difference. I'd expect [] to be faster, because it does not involve a global lookup followed by a function call.
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ] .
For your case the only difference is performance: append is twice as fast.
Python 3.0 (r30:67507, Dec 3 2008, 20:14:27) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import timeit >>> timeit.Timer('s.append("something")', 's = []').timeit() 0.20177424499999999 >>> timeit.Timer('s += ["something"]', 's = []').timeit() 0.41192320500000079 Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import timeit >>> timeit.Timer('s.append("something")', 's = []').timeit() 0.23079359499999999 >>> timeit.Timer('s += ["something"]', 's = []').timeit() 0.44208112500000141
In general case append
will add one item to the list, while +=
will copy all elements of right-hand-side list into the left-hand-side list.
Update: perf analysis
Comparing bytecodes we can assume that append
version wastes cycles in LOAD_ATTR
+ CALL_FUNCTION
, and += version -- in BUILD_LIST
. Apparently BUILD_LIST
outweighs LOAD_ATTR
+ CALL_FUNCTION
.
>>> import dis >>> dis.dis(compile("s = []; s.append('spam')", '', 'exec')) 1 0 BUILD_LIST 0 3 STORE_NAME 0 (s) 6 LOAD_NAME 0 (s) 9 LOAD_ATTR 1 (append) 12 LOAD_CONST 0 ('spam') 15 CALL_FUNCTION 1 18 POP_TOP 19 LOAD_CONST 1 (None) 22 RETURN_VALUE >>> dis.dis(compile("s = []; s += ['spam']", '', 'exec')) 1 0 BUILD_LIST 0 3 STORE_NAME 0 (s) 6 LOAD_NAME 0 (s) 9 LOAD_CONST 0 ('spam') 12 BUILD_LIST 1 15 INPLACE_ADD 16 STORE_NAME 0 (s) 19 LOAD_CONST 1 (None) 22 RETURN_VALUE
We can improve performance even more by removing LOAD_ATTR
overhead:
>>> timeit.Timer('a("something")', 's = []; a = s.append').timeit() 0.15924410999923566
In the example you gave, there is no difference, in terms of output, between append
and +=
. But there is a difference between append
and +
(which the question originally asked about).
>>> a = [] >>> id(a) 11814312 >>> a.append("hello") >>> id(a) 11814312 >>> b = [] >>> id(b) 11828720 >>> c = b + ["hello"] >>> id(c) 11833752 >>> b += ["hello"] >>> id(b) 11828720
As you can see, append
and +=
have the same result; they add the item to the list, without producing a new list. Using +
adds the two lists and produces a new list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With