Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Append multiple lists at once in Python
For various data analysis work in Python, we may need to combine many Python lists into one list. This helps process it as a single input for other parts of the program. It provides performance gains by reducing the number of loops required for processing the data further.
Using + Operator
The + operator does a straightforward job of joining lists together. We apply the operator between the names of the lists and store the final result in a new list. The sequence of elements in the lists is preserved.
Example
listA = ['Mon', 'Tue', 'Wed']
listB = ['2 pm', '11 am','1 pm']
listC = [1, 3, 6]
# Given lists
print("Given list A: ", listA)
print("Given list B: ", listB)
print("Given list C: ", listC)
# using + operator
res_list = listA + listB + listC
# printing result
print("Combined list is: ", res_list)
The output of the above code is −
Given list A: ['Mon', 'Tue', 'Wed'] Given list B: ['2 pm', '11 am', '1 pm'] Given list C: [1, 3, 6] Combined list is: ['Mon', 'Tue', 'Wed', '2 pm', '11 am', '1 pm', 1, 3, 6]
Using zip()
The zip function brings together elements from each of the lists at the same index position, then moves to the next index. This type of combining is useful when you want to preserve elements from the lists at the same index position together as tuples.
Example
listA = ['Mon', 'Tue', 'Wed']
listB = ['2 pm', '11 am','1 pm']
listC = [1, 3, 6]
# Given lists
print("Given list A: ", listA)
print("Given list B: ", listB)
print("Given list C: ", listC)
# using zip
res_list = list(zip(listA, listB, listC))
# printing result
print("Combined list is: ", res_list)
The output of the above code is −
Given list A: ['Mon', 'Tue', 'Wed']
Given list B: ['2 pm', '11 am', '1 pm']
Given list C: [1, 3, 6]
Combined list is: [('Mon', '2 pm', 1), ('Tue', '11 am', 3), ('Wed', '1 pm', 6)]
Using itertools.chain()
The chain function from the itertools module can bring the elements of the lists together while preserving the sequence in which they are present. It's memory-efficient as it creates an iterator.
Example
from itertools import chain
listA = ['Mon', 'Tue', 'Wed']
listB = ['2 pm', '11 am','1 pm']
listC = [1, 3, 6]
# Given lists
print("Given list A: ", listA)
print("Given list B: ", listB)
print("Given list C: ", listC)
# using chain
res_list = list(chain(listA, listB, listC))
# printing result
print("Combined list is: ", res_list)
The output of the above code is −
Given list A: ['Mon', 'Tue', 'Wed'] Given list B: ['2 pm', '11 am', '1 pm'] Given list C: [1, 3, 6] Combined list is: ['Mon', 'Tue', 'Wed', '2 pm', '11 am', '1 pm', 1, 3, 6]
Comparison
| Method | Result Type | Memory Usage | Best For |
|---|---|---|---|
+ operator |
Flat list | Creates new list | Simple concatenation |
zip() |
List of tuples | Creates new list | Grouping by index |
itertools.chain() |
Flat list | Memory-efficient | Large lists or many lists |
Conclusion
Use the + operator for simple list concatenation, zip() when you need to group elements by position, and itertools.chain() for memory-efficient combining of large lists. Each method serves different use cases depending on your data structure needs.
