Open In App

Merge Multiple Lists into one List

Python lists are versatile data structures that allow the storage of multiple elements in a single variable. While lists provide a convenient way to manage collections of data. In this article, we are going to learn how to merge multiple lists into One list.

Merge Multiple Lists Into One List in Python

Below are some of the ways by which we can merge multiple lists into one list in Python:



Merge Multiple Lists Into One List Using + Operator

In this method, we are going to use ( + ) Operator for merging all the list and we are going to store the resultant list in an empty list.




list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
 
# Merging the list
merged_list = list1 + list2 + list3
 
print("The Merged list is")
print(merged_list)

Output

The Merged list is
[1, 2, 3, 4, 5, 6, 7, 8, 9]


Merge Multiple Lists Into One List Using extend() Method

In this method we are going to use inbuilt extend() method of python. The extend() method adds the specified list elements. So we are appending all list element to a single resultant list.




# Method - 2 : Using extend() method
 
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
 
# Merging the list
merged_list = []
merged_list.extend(list1)
merged_list.extend(list2)
merged_list.extend(list3)
 
print("The Merged list is")
print(merged_list)

Output
The Merged list is
[1, 2, 3, 4, 5, 6, 7, 8, 9]


Merge Multiple Lists Into One List Using Loops

In this method, we are using loops for traversing each and every list element one by one and append all the list element to the resultant list.




# Method - 3 : Using loops
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
 
# Merging the list
merged_list = []
 
# Inserting List 1 data
for data in list1:
    merged_list.append(data)
     
# Inserting List 2 data
for data in list2:
    merged_list.append(data)
     
# Inserting List 3 data
for data in list3:
    merged_list.append(data)
 
print("The Merged list is")
print(merged_list)

Output
The Merged list is
[1, 2, 3, 4, 5, 6, 7, 8, 9]


Merge Multiple Lists Into One List Using extend() method and Loop

In this method, we are using the extend() method and loop both for merging all the list into single list.




lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 
merged_list = []
for sublist in lists:
    merged_list.extend(sublist)
 
print("The Merged list is")
print(merged_list)

Output
The Merged list is
[1, 2, 3, 4, 5, 6, 7, 8, 9]



Article Tags :