There may be situations while programming that we need to merge two or more lists in python. In this article, we will look at different ways with which we can merge two lists in python.
Merge lists using append() method in python
We can use the append() method to merge a list into another. The append() method is used to append new elements to an existing list. To merge two lists using the append() method, we will take a list and add elements from another list to the list one by one using a for loop. This can be done as follows.
list1=[1,2,3,4]
list2=[5,6,7,8]
print("First list is:")
print(list1)
print("Second list is:")
print(list2)
for i in list2:
list1.append(i)
print("Merged list is:")
print(list1)
Output:
First list is:
[1, 2, 3, 4]
Second list is:
[5, 6, 7, 8]
Merged list is:
[1, 2, 3, 4, 5, 6, 7, 8]
In the process of merging the lists using append() method, the first input list gets modified as elements from the second input list are added to it. Whereas there is no effect on the second list from which we are taking out elements to append in the first list.
Using append() and pop() method
Along with the append() method, we can also use pop() method to merge two lists in python. The pop() method when invoked on any list deletes the last element and returns it. We will use pop() method to take out elements from a list and will use append() method to add elements to the other list. This can be done as follows.
list1=[1,2,3,4]
list2=[5,6,7,8]
print("First list is:")
print(list1)
print("Second list is:")
print(list2)
while list2:
temp=list2.pop()
list1.append(temp)
print("Merged list is:")
print(list1)
Output:
First list is:
[1, 2, 3, 4]
Second list is:
[5, 6, 7, 8]
Merged list is:
[1, 2, 3, 4, 8, 7, 6, 5]
While using the above method to merge two lists, both the input lists get modified. The first input list gets modified as elements from other lists are added to the first list. All the elements from the second list are deleted using pop() method, hence the second list becomes empty after merging the lists. In the output we can also see that the elements of second list appear in reversed order in the merged list.
Using list comprehension
We can also use list comprehension to merge two or more lists in python. For this we will first create a list of all the lists to be merged and then we can use list comprehension to create the merged list as follows.
list1=[1,2,3,4]
list2=[5,6,7,8]
print("First list is:")
print(list1)
print("Second list is:")
print(list2)
combined_list=[list1,list2]
merged_list=[item for sublist in combined_list for item in sublist]
print("Merged list is:")
print(merged_list)
Output:
First list is:
[1, 2, 3, 4]
Second list is:
[5, 6, 7, 8]
Merged list is:
[1, 2, 3, 4, 5, 6, 7, 8]
When we merge lists using list comprehension, none of the input lists which are to be merged will get modified.
Merge lists using + operator in python
We can directly merge two or more lists using + operator by simply adding all the lists using + operator as follows.
list1=[1,2,3,4]
list2=[5,6,7,8]
print("First list is:")
print(list1)
print("Second list is:")
print(list2)
merged_list=list1+list2
print("Merged list is:")
print(merged_list)
Output:
First list is:
[1, 2, 3, 4]
Second list is:
[5, 6, 7, 8]
Merged list is:
[1, 2, 3, 4, 5, 6, 7, 8]
When we merge two lists using + operator as shown in above program, none of the input lists which are being merged will get modified.
Using asterisk operator
We can use packing operator * to pack two or more lists together to merge the lists as follows.
list1=[1,2,3,4]
list2=[5,6,7,8]
print("First list is:")
print(list1)
print("Second list is:")
print(list2)
merged_list=[*list1,*list2]
print("Merged list is:")
print(merged_list)
Output:
First list is:
[1, 2, 3, 4]
Second list is:
[5, 6, 7, 8]
Merged list is:
[1, 2, 3, 4, 5, 6, 7, 8]
When we use asterisk operator to merge two lists, none of the input lists which are being merged will get modified. This method can also be handy for merging three or more lists at once as we can include elements from each list in the output merged list by simply mentioning each list using * operator.
Using extend() method
To merge two or more lists using extend() method, we can perform in place extension of the first list by adding elements of other lists to it as follows.
list1=[1,2,3,4]
list2=[5,6,7,8]
print("First list is:")
print(list1)
print("Second list is:")
print(list2)
list1.extend(list2)
print("Merged list is:")
print(list1)
Output:
First list is:
[1, 2, 3, 4]
Second list is:
[5, 6, 7, 8]
Merged list is:
[1, 2, 3, 4, 5, 6, 7, 8]
When we merge two lists using extend() method, the list on which extend() method gets modified as elements from other lists are added to it. Other lists are not affected by the operation.
Merge lists using itertools.chain() method in python
We can also use itertools.chain() method to merge two or more lists in python. For this, we will pass the lists as arguments to itertools.chain() method and the method returns an iterable containing all the elements of the lists to be merged which can be further converted into a list This can be done as follows.
import itertools
list1=[1,2,3,4]
list2=[5,6,7,8]
print("First list is:")
print(list1)
print("Second list is:")
print(list2)
merged_list=list(itertools.chain(list1,list2))
print("Merged list is:")
print(merged_list)
Output:
First list is:
[1, 2, 3, 4]
Second list is:
[5, 6, 7, 8]
Merged list is:
[1, 2, 3, 4, 5, 6, 7, 8]
When we merge lists using itertools.chain() method, none of the input lists get modified. This way to merge lists in python can be handy to merge more than two lists as we just have to pass the input lists as parameters to itertools.chain() method.
Conclusion
In this article, we have seen various ways to merge two or more lists in python using different methods and modules. We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.