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. In this article, we will learn about Python Append Multiple Items to List at Once.
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. there are various ways to Append multiple lists at once in Python here we are discussing some generally used methods for appending multiple lists at once in Python. those are for the lowing.
- Using extend() Method
- Using the + Operator
- Using
zip()
and sum()
- Using a List Method
- Using List Comprehension
- Using reduce() Function
- Using itertools.chain() Function
- Using the append() Method in a Loop
- Using reduce() and operator.add() Functions
Create Multiple List
In this example the below code initializes three lists (`test_list1`, `test_list2`, and `test_list3`) and prints their original content.
Python3
test_list1 = [ 1 , 3 , 5 , 5 , 4 ]
test_list2 = [ 4 , 6 , 2 , 8 , 10 ]
test_list3 = [ 7 , 5 , 2 , 9 , 11 ]
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))
|
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]
Python Append Multiple Items to List at Once 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.
Example : In this example the below code uses list comprehension to extend `test_list1` by appending elements from `test_list2` and `test_list3`, and then prints the modified list.
Python3
[test_list1.extend(l) for l in (test_list2,test_list3)]
print ( "The extended and modified list is : " + str (test_list1))
|
Output:
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)
Append Multiple Items to a Lists in Python using the + Operator
This can be easily done using the + Arithmetic 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.
Example : In this example the below code combines three lists (`test_list1`, `test_list2`, `test_list3`) using the `+` operator, creating a new extended and modified list, which is then printed.
Python3
test_list1 = test_list1 + test_list2 + test_list3
print ( "The extended and modified list is : " + str (test_list1))
|
Output:
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)
Append Multiple Lists at Once in Python using zip()
and sum()
In this method code uses the `zip()` function to pair corresponding elements from multiple lists together, creating tuples. Then, the `sum()` function is employed to concatenate these tuples into a single iterable.
Example : In this example the below code combines corresponding elements from three lists (`test_list1`, `test_list2`, `test_list3`) using `zip()` and flattens the result into a single list using `sum()`. The combined list is then printed.
Python3
res = sum ( zip (test_list1, test_list2, test_list3), ())
print ( "The combined list using zip() and sum() : " + str (res))
|
Output :
The combined list using zip() and sum() : (1, 4, 7, 3, 6, 5, 5, 2, 2, 5, 4, 8, 9, 10, 11)
Append Multiple Lists at Once in Python using a
List
To append multiple lists at once in Python using a list, you can employ the `extend()` method. First, initialize an empty list (`res`). Then, use the `extend()` method to append each individual list to the empty list sequentially.
Example : In this example the below code creates an empty list `res` and appends the elements of three separate lists (`test_list1`, `test_list2`, `test_list3`) into it using the `extend()` method, resulting in a combined list, which is then printed.
Python3
res = []
res.extend(test_list1)
res.extend(test_list2)
res.extend(test_list3)
print ( "The combined list using extend() and a list : " + str (res))
|
Output :
The combined list using extend() and a list : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]
Time Complexity: O(n)
Space Complexity: O(n)
Python Append Items Elements to List using List Comprehension
List comprehension in Python provides a concise way to append multiple lists simultaneously. By utilizing a nested structure within list comprehension, you can iterate through each list and append its elements to a new list.
Example : In this example the below code uses list comprehension to concatenate three lists (`test_list1`, `test_list2`, `test_list3`) into a single list (`res`), and then prints the result. The combined list contains elements from all three original lists.
Python3
res = [i for j in [test_list1, test_list2, test_list3] for i in j]
print ( "The combined list using list comprehension : " + str (res))
|
Output :
The combined list using list comprehension : [1, 3, 5, 5, 4, 4, 6, 2, 8, 10, 7, 5, 2, 9, 11]
Time Complexity: O(n)
Space Complexity: O(n)
Append Multiple Lists at Once in Python 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.
Example : In this example the below code uses the `functools.reduce` function with a lambda function to concatenate `test_list1` with the elements of `[test_list2, test_list3]`, resulting in an extended and modified list, which is then printed.
Python3
import functools as f
test_list1 = f. reduce ( lambda test_list1, test_list2:
test_list1 + test_list2, [test_list2, test_list3],
test_list1)
print ( "The extended and modified list is : " + str (test_list1))
|
Output:
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)
Append Multiple Items to a List in Python 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.
Example : In this example the below code uses `itertools.chain()` to efficiently combine multiple lists (`test_list1`, `test_list2`, `test_list3`) into a single list, and then converts the combined iterable into a list. The result is printed as the extended and modified list.
Python3
from itertools import chain
test_list1 = list (chain(test_list1, test_list2, test_list3))
print ( "The extended and modified list is : " + str (test_list1))
|
Output:
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)
Append Multiple Lists at Once in Python using the append() Method
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.
Example : In this example the below code iterates over the elements of `test_list2` and `test_list3`, appending each element to `test_list1`, and then prints the resulting extended and modified list.
Python3
for l in (test_list2, test_list3):
for element in l:
test_list1.append(element)
print ( "The extended and modified list is:" , test_list1)
|
Output:
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)
Append Multiple Lists at Once in Python 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.
Example : In this example the below code uses `functools.reduce()` and `operator.add()` to concatenate multiple lists (`test_list1`, `test_list2`, `test_list3`) into a single list (`final_list`), and then prints the combined result.
Python3
import functools
import operator
final_list = functools. reduce (operator.add, [test_list1, test_list2, test_list3])
print ( "The extended and modified list is : " + str (final_list))
|
Output:
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)
Share your thoughts in the comments
Please Login to comment...