Computer >> Computer tutorials >  >> Programming >> Python

What is the difference between working of append and + operator in a list in Python?


The + operator creates a new list in python when 2 lists are combined using it, the original object is not modified. On the other hand, using methods like extend and append, we add the lists in place, ie, the original object is modified. Also using append inserts the list as a object while + just concatenates the 2 lists. 

example

list1 = [1, 2, 3]
list2 = ['a', 'b']
list3 = list1 + list2
print(list3)

Output

This will give the output −

[1, 2, 3, 'a', 'b']

When using append −

Example

list1 = [1, 2, 3]
list2 = ['a', 'b']
list1.append(list2)
print(list1)

Output

This will give the output −

[1, 2, 3, ['a', 'b']]