Python list append() vs extend() method
In Python, list is the most versatile data structure that stores the elements inside [] brackets separated by comma(,).
Once we create a list, we may need to add new elements to it on some events. This elements can be added at the end, beginning, or somewhere in between. The list offers three different methods to add an elements in the existing list. They are insert()
, append()
and extend()
methods. Here, we will differentiate between insertion using the append()
and the extend()
methods of list.
append() vs extend() method compared
The table below shows the difference between append()
and extend()
method in list.
append() method | extend() method |
---|---|
Adds an item to the end of the list. | Extend the list by adding all the items from the iterable. |
Syntax : listname.append(x) | Syntax : listname.extend(iterable) |
Parameter can be an individual element or a n iterable. | Parameter is always an iterable. |
Size of the list will increase by 1. | Size of the list will increase by the number of elements in the iterable passed as a parameter. |
Used for adding one element at a time. | Used for inserting the large amount of elements to the existing list. |
Suitable for insertion of string elements to the list. | It is not suitable for string insertion as each characters will be inserted as a individual elements in the list. |
Examples using append() vs extend() method
Example 1 : Adding item using append() method
In this example, we are appending one element to the existing list.
# Initializing the list and a variable
l=[10,20,30]
x=40
# Printing the list
print("List before append",l)
# Using the append method and printing
l.append(x)
print("List after append",l)
Output:
List before append [10, 20, 30]
List after append [10, 20, 30, 40]
Example 2 : Adding item using extend() method
In this example, we are appending multiple elements from another list to this existing list.
# Initializing the lists
l1=[10,20,30]
l2=[40,50,60]
# Printing the list
print("List before extend",l1)
# Using the extend method and printing
l1.extend(l2)
print("List after extend",l1)
Output:
List before extend [10, 20, 30]
List after extend [10, 20, 30, 40, 50, 60]
Example 3 : Comparing append() vs extend() method
In this example, we are appending multiple elements from another list to this existing list.
# Initializing the list and a variable
l1=[10,20,30]
l2=[10,20,30]
l3=[40,50,60]
# Printing the list
print("List l1 =",l1)
print("List l2 =",l2)
print("List l3 =",l3)
# Using the extend method and printing
l1.extend(l3)
print("The result of l1.extend(l3)",l1)
# Now using append() method
l2.append(l3)
print("The result of l2.append(l3)",l2)
Output:
List l1 = [10, 20, 30]
List l2 = [10, 20, 30]
List l3 = [40, 50, 60]
The result of l1.extend(l3) [10, 20, 30, 40, 50, 60]
The result of l2.append(l3) [10, 20, 30, [40, 50, 60]]
Example 4 : Adding tuple - append() vs extend() method
In this example, we are inserting elements from the tuple using append and extend method. Also, after insertion we will print the length of this lists.
# Initializing
l1=["C","C++","Java"]
l2=["C","C++","Java"]
l3=("Python", "R","Perl")
# Printing the list
print("List l1 =",l1)
print("List l2 =",l2)
print("List l3 =",l3)
# Using the extend method and printing
l1.extend(l3)
print("The result of l1.extend(l3)",l1)
print("The size of list l1 is ",len(l1))
# Now using append() method
l2.append(l3)
print("The result of l2.append(l3)",l2)
print("The size of list l2 is ",len(l2))
Output:
List l1 = ['C', 'C++', 'Java']
List l2 = ['C', 'C++', 'Java']
List l3 = ('Python', 'R', 'Perl')
The result of l1.extend(l3) ['C', 'C++', 'Java', 'Python', 'R', 'Perl']
The size of list l1 is 6
The result of l2.append(l3) ['C', 'C++', 'Java', ('Python', 'R', 'Perl')]
The size of list l2 is 4
Example 5 : Adding dictionary - append() vs extend() method
In this example, we are inserting elements from the dictionary using append and extend method. Also, after insertion we will print the length of this lists.
# Initializing
l1=["C","C++","Java"]
l2=["C","C++","Java"]
l3={1:"Python",2:"R"}
# Printing the list
print("List l1 =",l1)
print("List l2 =",l2)
print("List l3 =",l3)
# Using the extend method and printing
l1.extend(l3)
print("The result of l1.extend(l3)",l1)
print("The size of list l1 is ",len(l1))
# Now using append() method
l2.append(l3)
print("The result of l2.append(l3)",l2)
print("The size of list l2 is ",len(l2))
Output:
List l1 = ['C', 'C++', 'Java']
List l2 = ['C', 'C++', 'Java']
List l3 = {1: 'Python', 2: 'R'}
The result of l1.extend(l3) ['C', 'C++', 'Java', 1, 2]
The size of list l1 is 5
The result of l2.append(l3) ['C', 'C++', 'Java', {1: 'Python', 2: 'R'}]
The size of list l2 is 4
Example 6 : Adding String - append() vs extend() method
In this example, we are inserting a string to the existing list using append and extend method. Also, after insertion we will print the length of this lists. As we can see, each character of a string is inserted as a individual elements if we use extend method. So, the append()
method is suitable to insert strings in the list.
# Initializing
l1=["C","C++","Java"]
l2=["C","C++","Java"]
l3="Python Programming"
# Printing the list
print("List l1 =",l1)
print("List l2 =",l2)
print("List l3 =",l3)
# Using the extend method and printing
l1.extend(l3)
print("The result of l1.extend(l3)",l1)
print("The size of list l1 is ",len(l1))
# Now using append() method
l2.append(l3)
print("The result of l2.append(l3)",l2)
print("The size of list l2 is ",len(l2))
Output:
List l1 = ['C', 'C++', 'Java']
List l2 = ['C', 'C++', 'Java']
List l3 = Python Programming
The result of l1.extend(l3) ['C', 'C++', 'Java', 'P', 'y', 't', 'h', 'o', 'n', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']
The size of list l1 is 21
The result of l2.append(l3) ['C', 'C++', 'Java', 'Python Programming']
The size of list l2 is 4
Summary
The knowledge of append()
vs extend()
method in list is very useful while working on real time applications in Python. In many situations, we will need to merge the content of multiple data structure to form the one. But, depending upon the structure of data and type of values it is necessary to make an appropriate selection of either the append()
method or an extend()
method. In this tutorial, we covered different examples taking different real time scenarios. As per the requirement of an application, we can choose an appropriate approach for merging.
We learned in detail about this with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on append()
vs extend()
a list in Python.
References