Ways to concatenate two lists in Python
Let’s see how to concatenate two lists using different methods in Python. This operation is useful when we have number of lists of elements which need to be processed in a similar manner.
Method #1 : 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
# Python3 code to demonstrate list # concatenation using naive method # 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)) |
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)
Method #2 : Using + 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
# Python 3 code to demonstrate list # concatenation using + operator # 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)) |
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.
Method #3: 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)) |
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 length of res_list.
Method #4: Using extend() extend() is the function extended by lists in Python and hence can be used to perform this task. This function performs the inplace extension of 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)) |
Concatenated list using list.extend() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #5: Using * operator Using * operator, this method is the 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)) |
Concatenated list using * operator : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #6: Using itertools.chain() itertools.chain() returns the iterable after chaining its arguments in one and hence does not require to store the concatenated list if only its initial iteration is required. This is useful when 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)) |
Concatenated list using itertools.chain() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Method #7: ‘+=’ Operator:
Python3
test_list1 = [ 1 , 4 , 5 , 6 , 5 ] test_list2 = [ 3 , 5 , 7 , 2 , 5 ] concatenated_list = test_list1 + test_list2 print ( "Concatenated list using += operator:" , concatenated_list) #This is code contributed by Jyothi pinjala. |
Concatenated list using += operator: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #8: Using reduce() function:
In this approach, we will use the reduce() function of the functools library of Python.
- 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 previous step. We need to form a nested list.
- Now we will use the reduce() function and pass that nested list as parameter alongside two variables (if we choose to have two lists). And use the Anonymous function lambda to do the concatenation operation and storing it in a list.
- Finally we will print it.
Below is the implementation of the above approach:
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,[])) |
[1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
Time Complexity: O(n+m) #n is the length of first list, m is the length of second list.
Auxiliary Space: O(n) #n is the number of lists taken into consideration
Please Login to comment...