Open In App

Merge Two Lists in Python

Last Updated : 07 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Let’s see how to concatenate two lists using different methods in Python. This operation is useful when we have a number of lists of elements that need to be processed in a similar manner.

Input:     test_list1 = [1, 4, 5, 6, 5]
test_list2 = [3, 5, 7, 2, 5]
Output: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Explanation: The output list is the list we get from Merging both the input list.

Python Join Two Lists

Below are the methods that we will cover in this article:

Merge two lists in Python using Naive Method 

In this method, we traverse the second list and keep appending elements in the first list, so that the first list would have all the elements in both lists and hence would perform the append. 

Python3




# Initializing lists
test_list1 = [1, 4, 5, 6, 5]
test_list2 = [3, 5, 7, 2, 5]
 
# using naive method to concat
for i in test_list2 :
    test_list1.append(i)
 
# Printing concatenated list
print ("Concatenated list using naive method : "
                            + str(test_list1))


Output

Concatenated list using naive method : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Time Complexity: O(n + m), where n and m are the lengths of the given test_list1 and test_list2 respectively.
Auxiliary Space: O(m)

Concatenate two lists using the “+” operator 

The most conventional method to perform the list concatenation, the use of “+” operator can easily add the whole of one list behind the other list and hence perform the concatenation. 

Python3




# Initializing lists
test_list3 = [1, 4, 5, 6, 5]
test_list4 = [3, 5, 7, 2, 5]
 
# using + operator to concat
test_list3 = test_list3 + test_list4
 
# Printing concatenated list
print ("Concatenated list using + : "
                + str(test_list3))


Output

Concatenated list using + : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Time complexity: O(n), where n is the total number of elements in both lists, as the + operator iterates through all elements of both lists to concatenate them.
Auxiliary space: O(n), where n is the total number of elements in both lists, as a new list is created to store the concatenated list.

Merge Two Lists in Python using list comprehension

List comprehension can also accomplish this task of list concatenation. In this case, a new list is created, but this method is a one-liner alternative to the loop method discussed above. 

Python3




# Python3 code to demonstrate list
# concatenation using list comprehension
 
# Initializing lists
test_list1 = [1, 4, 5, 6, 5]
test_list2 = [3, 5, 7, 2, 5]
 
# using list comprehension to concat
res_list = [y for x in [test_list1, test_list2] for y in x]
 
# Printing concatenated list
print ("Concatenated list using list comprehension: "
                                    + str(res_list))


Output

Concatenated list using list comprehension: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Time Complexity: O(n + m), where n and m are length of given test_list1 and test_list2
Auxiliary Space: O(k), where k is the length of res_list.  

Merge two lists using extend() 

The extend() is the function extended by lists in Python and hence can be used to perform this task. This function performs the in-place extension of the first list. 

Python3




# Python3 code to demonstrate list
# concatenation using list.extend()
 
# Initializing lists
test_list3 = [1, 4, 5, 6, 5]
test_list4 = [3, 5, 7, 2, 5]
 
# using list.extend() to concat
test_list3.extend(test_list4)
 
# Printing concatenated list
print ("Concatenated list using list.extend() : "
                            + str(test_list3))


Output

Concatenated list using list.extend() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Python Join Two Lists using * operator 

Using the * operator, this method is a new addition to list concatenation and works only in Python 3.6+. Any no. of lists can be concatenated and returned in a new list using this operator. 

Python3




# Python3 code to demonstrate list
# concatenation using * operator
 
# Initializing lists
test_list1 = [1, 4, 5, 6, 5]
test_list2 = [3, 5, 7, 2, 5]
 
# using * operator to concat
res_list = [*test_list1, *test_list2]
 
# Printing concatenated list
print ("Concatenated list using * operator : "
                            + str(res_list))


Output

Concatenated list using * operator : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Python Join Two Lists using itertools.chain()

The itertools.chain() returns the iterable after chaining its arguments in one and hence does not require storing the concatenated list if only its initial iteration is required. This is useful when the concatenated list has to be used just once. 

Python3




# Python3 code to demonstrate list
# concatenation using itertools.chain()
import itertools
 
# Initializing lists
test_list1 = [1, 4, 5, 6, 5]
test_list2 = [3, 5, 7, 2, 5]
 
# using itertools.chain() to concat
res_list = list(itertools.chain(test_list1, test_list2))
 
# Printing concatenated list
print ("Concatenated list using itertools.chain() : "
                                    + str(res_list))


Output

Concatenated list using itertools.chain() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Concatenate two lists using reduce() function 

Firstly we need to import the reduce function from functools library. Then initialize two variables that hold two different lists. Now we will use another list in which we will store all the lists taken in the previous step. We need to form a nested list. Now we will use the reduce() function and pass that nested list as a parameter alongside two variables (if we choose to have two lists). And use the Anonymous function lambda to do the concatenation operation and store it in a list.

Python3




from functools import reduce
 
test_list1 = [1, 4, 5, 6, 5]
test_list2 = [3, 5, 7, 2, 5]
 
nested_list = [test_list1,test_list2]
print(reduce(lambda i,j:i+j,nested_list,[]))


Output

[1, 4, 5, 6, 5, 3, 5, 7, 2, 5]

Time Complexity: O(n+m), n is the length of the first list, and m is the length of the second list.
Auxiliary Space: O(n), n is the number of lists taken into consideration



Previous Article
Next Article

Similar Reads

Python | Merge two lists into list of tuples
Given two lists, write a Python program to merge the two lists into list of tuples. Examples: Input : list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] Output : [(1, 'a'), (2, 'b'), (3, 'c')] Input : list1 = [1, 2, 3, 4] list2 = [ 1, 4, 9] Output : [(1, 1), (2, 4), (3, 9), (4, '')]Approach #1: Naive Here we will merge both the list into a list of tuples us
6 min read
Python | Merge corresponding sublists from two different lists
Given two lists containing sublists, write a Python program to merge the two sublists based on same index. Examples: Input : l1 = [['+', '-', '+'], ['-', '+'], ['+', '-', '+']] l2 = [['2a3', 'b2', '3c'], ['a3', 'b2'], ['a3', '2b2', '5c']] Output : [['+2a3', '-b2', '+3c'], ['-a3', '+b2'], ['+a3', '-2b2', '+5c']] Input : l1 = [['1', '2'], ['1', '2',
5 min read
Python | Merge two lists alternatively
Given two lists, write a Python program to merge the given lists in an alternative fashion, provided that the two lists are of equal length. Examples: Input : lst1 = [1, 2, 3] lst2 = ['a', 'b', 'c'] Output : [1, 'a', 2, 'b', 3, 'c'] Input : lst1 = ['name', 'alice', 'bob'] lst2 = ['marks', 87, 56] Output : ['name', 'marks', 'alice', 87, 'bob', 56] M
4 min read
Python | Merge two list of lists according to first element
Given two list of lists of equal length, write a Python program to merge the given two lists, according to the first common element of each sublist. Examples: Input : lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']] lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']] Output : [[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]Input
6 min read
Python Merge Two Lists Without Duplicates
In Python coding, combining two lists without repeating elements can be a tricky task. It involves skillfully managing data structures to achieve a smooth and effective outcome. In this short guide, we'll explore ways to merge lists in Python, making sure duplicates are handled seamlessly. Python Merge Two Lists Without DuplicatesBelow, are the met
3 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Merge Key Value Lists into Dictionary Python
Sometimes, while working with lists, we can come forward with a problem in which we need to perform the merge function in which we have the key list and need to create dictionary mapping keys with corresponding value in other list. Let's discuss certain ways in which this task can be performed. Merge Key Value Lists into Dictionary Python Using zip
8 min read
Python | Merge List with common elements in a List of Lists
Given a list of list, we have to merge all sub-list having common elements. These type of problems are very frequent in College examinations and while solving coding competitions. Below are some ways to achieve this. Input: [[11, 27, 13], [11, 27, 55], [22, 0, 43], [22, 0, 96], [13, 27, 11], [13, 27, 55], [43, 0, 22], [43, 0, 96], [55, 27, 11]] Out
3 min read
Python | Merge overlapping part of lists
Sometimes, while working with Python lists, we can have a problem in which we have to merge two lists' overlapping parts. This kind of problem can come in day-day programming domain. Let's discuss a way in which this problem can be solved. Method 1: Using generator + next() + list slicing This method can be employed to solve this task. In this, fir
6 min read
Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
Practice Tags :
three90RightbarBannerImg