Merge Multiple Lists into one List
Last Updated :
14 Feb, 2024
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:
- Using + Operator
- Using extend() Method
- Using Loops
- Using extend() method in a loop
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.
Python3
list1 = [ 1 , 2 , 3 ]
list2 = [ 4 , 5 , 6 ]
list3 = [ 7 , 8 , 9 ]
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.
Python3
list1 = [ 1 , 2 , 3 ]
list2 = [ 4 , 5 , 6 ]
list3 = [ 7 , 8 , 9 ]
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.
Python3
list1 = [ 1 , 2 , 3 ]
list2 = [ 4 , 5 , 6 ]
list3 = [ 7 , 8 , 9 ]
merged_list = []
for data in list1:
merged_list.append(data)
for data in list2:
merged_list.append(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.
Python3
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]
Share your thoughts in the comments
Please Login to comment...