Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Append multiple lists at once

Python | Append multiple lists at once

There can be an application requirement to append elements of 2-3 lists to one list in Python. This kind of application has the potential to come into the domain of Machine Learning or sometimes in web development as well.

Example:

Input:
list1 = [1, 3, 5, 5, 4]
list2 = [4, 6, 2, 8, 10]
list3 = [7, 5, 2, 9, 11]

Output:
[1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]

Append Multiple Lists at once in Python

Let’s discuss certain ways in which we can append multiple lists at once in Python programming.

Using extend() Method

Here we are using the concept of Python list comprehension and the extend() function to append all the elements of the other lists at once in the first list.

Python3




# initializing lists
test_list1 = [1, 3, 5, 5, 4]
test_list2 = [4, 6, 2, 8, 10]
test_list3 = [7, 5, 2, 9, 11]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
print ("The original list 3 is : " + str(test_list3))
 
#using list comprehension
[test_list1.extend(l) for l in (test_list2,test_list3)]
 
print ("The extended and modified list is : " + str(test_list1))


Output:

The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The extended and modified list is : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]

Time Complexity: O(N)
Space Complexity: O(N)

Using the + Operator

This can be easily done using the + Arithimetic operator as it does the element addition at the back of the list in Python. Similar logic is extended in the case of multiple lists.

Python3




# initializing lists
test_list1 = [1, 3, 5, 5, 4]
test_list2 = [4, 6, 2, 8, 10]
test_list3 = [7, 5, 2, 9, 11]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
print ("The original list 3 is : " + str(test_list3))
 
# using + operator
# adding multiple list at once
test_list1 = test_list1 + test_list2 + test_list3
     
# printing result
print ("The extended and modified list is : " +  str(test_list1))


Output:

The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The extended and modified list is : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]

Time Complexity: O(N)
Auxiliary Space: O(N)

Using reduce() Function

Here we are using the reduce() function of the Python functools module. The reduce() function will take a lambda function to extend the lists and with this approach, we can add as many lists to the original list in Python.

Python3




import functools as f
 
# initializing lists
test_list1 = [1, 3, 5, 5, 4]
test_list2 = [4, 6, 2, 8, 10]
test_list3 = [7, 5, 2, 9, 11]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
print ("The original list 3 is : " + str(test_list3))
 
# using lambda function
test_list1 = f.reduce(lambda test_list1, test_list2:
                      test_list1+test_list2, [test_list2, test_list3],
                      test_list1)
# printing result
print ("The extended and modified list is : " +  str(test_list1))


Output:

The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The extended and modified list is : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]

Time complexity: O(n), where n is the total number of elements in all three lists. 
Auxiliary space: O(n), as it creates a new list that contains all the elements from test_list1, test_list2, and test_list3.

Using itertools.chain() Function

The chain() function from the itertools module can also be employed to append multiple list in Python as it uses the iterator to perform this and hence offers better performance over the above method.

Python3




from itertools import chain
 
# initializing lists
test_list1 = [1, 3, 5, 5, 4]
test_list2 = [4, 6, 2, 8, 10]
test_list3 = [7, 5, 2, 9, 11]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
print ("The original list 3 is : " + str(test_list3))
 
# using itertools.chain()
# adding multiple list at once
test_list1 = list(chain(test_list1, test_list2, test_list3))
     
# printing result
print ("The extended and modified list is : " +  str(test_list1))


Output:

The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The extended and modified list is : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]

Time complexity: O(n), where n is the total number of elements in all three lists. 
Auxiliary space: O(n), as it creates a new list that contains all the elements from test_list1, test_list2, and test_list3.

Using the append() Method in a Loop

In this method, we will append list 2 and 3 to the first list by using a Python for loop to iterate over each element of list 2 and list 3 to append them to the list 1 using the append() method.

Python3




#Initializing three lists
test_list1 = [1, 3, 5, 5, 4]
test_list2 = [4, 6, 2, 8, 10]
test_list3 = [7, 5, 2, 9, 11]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
print ("The original list 3 is : " + str(test_list3))
 
#Loop over the other two lists and append each element to the first list
for l in (test_list2, test_list3):
    for element in l:
        test_list1.append(element)
 
#Print the extended and modified list
print("The extended and modified list is:", test_list1)


Output:

The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The extended and modified list is : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]

Time complexity:

  1. The time complexity of this algorithm is O(n), where n is the total number of elements in all three lists.
  2. The nested for loop will iterate over all elements in test_list2 and test_list3, so the time complexity will depend on the length of these lists.

Auxiliary space:

  1. The auxiliary space complexity of this algorithm is O(n), where n is the total number of elements in all three lists.
  2. The extended_list will store all the elements from the three lists, so its size will depend on the length of the lists.
  3. The other variables (test_list1, test_list2, test_list3, l, element) take constant space and do not depend on the size of the input.

Using reduce() and operator.add() Functions

We can use the reduce() function from the functools module and the add() operator from the operator module to add all the lists to a single list. Just pass the operater.add function and the lists to be appended to the reduce function.

Python3




# importing modules
import functools
import operator
 
# initializing lists
test_list1 = [1, 3, 5, 5, 4]
test_list2 = [4, 6, 2, 8, 10]
test_list3 = [7, 5, 2, 9, 11]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
print("The original list 3 is : " + str(test_list3))
 
# using functools.reduce() and operator.add() to append multiple lists at once
final_list = functools.reduce(operator.add, [test_list1, test_list2, test_list3])
 
# printing the final list
print("The extended and modified list is : " + str(final_list))


Output:

The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The extended and modified list is : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]

Time Complexity: O(n), where n is the total number of elements in all three lists.
Space Complexity: O(n), as it creates a new list that contains all the elements from test_list1, test_list2 and test_list3.

RELATED ARTICLES

Most Popular

Recent Comments