Another benefit of .extend
is that you can call it on a global list because that merely mutates the list, whereas +=
won't work in that context because you can't assign to a global in the local scope.
Demo
a_list = ['one']
list2 = ['two', 'three']
def f():
a_list.extend(list2)
def g():
a_list += list2
f()
print(a_list)
g()
print(a_list)
output
['one', 'two', 'three']
Traceback (most recent call last):
File "./qtest.py", line 43, in <module>
g()
File "./qtest.py", line 39, in g
a_list += list2
UnboundLocalError: local variable 'a_list' referenced before assignment
However, you can use +=
if you also use slice assignment since that's also a mutation of the original list:
a_list = ['one']
list2 = ['two', 'three']
def g():
a_list[:] += list2
g()
print(a_list)
output
['one', 'two', 'three']