Python - Append Multiple Lists at Once
Last Updated :
14 Feb, 2025
Our task is to merge multiple lists into a single list by appending all their elements. For example, if the input is a = [1, 2], b = [3, 4], and c = [5, 6], the output should be [1, 2, 3, 4, 5, 6].
itertools.chain() function from the itertools module is another way to combine lists efficiently. It creates an iterator that combines multiple lists.
Python
import itertools
a = [1, 2]
b = [3, 4]
c = [5, 6]
e = list(itertools.chain(a, b, c))
# e contains [1, 2, 3, 4, 5, 6]
print(e)
Explanation:
- itertools.chain(a, b, c) efficiently iterates over all lists without creating intermediate copies, merging them into a single sequence.
- list(itertools.chain(a, b, c)) converts the chained sequence into a list, storing the merged result in e.
Other methods that we can use to append multiple lists at once are:
Using the extend() Method
One of the easiest ways to append multiple lists is by using the extend() method as this method allows us to add elements from one list to another list. ( Note: It modifies the original list in place.)
Python
a = [1, 2]
b = [3, 4]
c = [5, 6]
a.extend(b)
a.extend(c)
# a now contains [1, 2, 3, 4, 5, 6]
print(a)
Explanation: Here we extend list a by adding elements of list b and then list c.
Using the + Operator
Another simple way to append multiple lists is by using the + operator. It creates a new list by concatenating the lists.
Python
a = [1, 2]
b = [3, 4]
c = [5, 6]
res = a + b + c
# result contains [1, 2, 3, 4, 5, 6]
print(res)
Explanation: a + b + c concatenates all three lists into a single list, the result is stored in res and printed as [1, 2, 3, 4, 5, 6].
Using List Comprehension
We can also use list comprehension to append multiple lists in a very Pythonic way. It gives us full control over how we combine the lists.
Python
a = [1, 2]
b = [3, 4]
c = [5, 6]
p = [x for lst in [a, b, c] for x in lst]
# p contains [1, 2, 3, 4, 5, 6]
print(p)
Explanation:
- list comprehension [x for lst in [a, b, c] for x in lst] iterates over each list (a, b, c), then over each element in those lists, flattening them into a single list.
- final merged list is stored in p and printed as [1, 2, 3, 4, 5, 6].
Using zip()
This method is a bit more advanced but can be useful in specific cases. We can use the zip() function to group elements from multiple lists to combine them into a single list.
Python
a = [1, 2]
b = [3, 4]
c = [5, 6]
# Use a list comprehension to flatten the zipped lists
e = [item for sublist in zip(a, b, c) for item in sublist]
# e contains [1, 3, 5, 2, 4, 6]
print(e)
Explanation:
zip(a, b, c)
groups elements from a
, b
, and c
into tuples like (1, 3, 5)
, (2, 4, 6)
.- list comprehension
[item for sublist in zip(a, b, c) for item in sublist]
flattens these tuples into a single list, resulting in [1, 3, 5, 2, 4, 6]
.
Using the append() Method in a Loop
append() method can also be used in a loop to add elements from multiple lists to an empty list. This method is slower than others because it adds one element at a time.
Python
a = [1, 2]
b = [3, 4]
c = [5, 6]
s = []
for lst in [a, b, c]:
for item in lst:
s.append(item)
# s contains [1, 2, 3, 4, 5, 6]
print(s)
Similar Reads
Append Multiple items to List - Python Appending items to a list in Python is an essential and common operation when working with different data structures. Sometimes, we need to add more than one item to a list at a time and in this article, we will explore the various ways to append multiple items to a list at the same time.Using exten
2 min read
How To Combine Multiple Lists Into One List Python Combining multiple lists into a single list is a common operation in Python, and there are various approaches to achieve this task. In this article, we will see how to combine multiple lists into one list in Python. Combine Multiple Lists Into One List PythonBelow are some of the ways by which we ca
2 min read
Python - Append Multiple elements in set In Python, sets are an unordered and mutable collection of data type what does not contains any duplicate elements. In this article, we will learn how to append multiple elements in the set at once. Example: Input: test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 10]Output: {1, 2, 4, 5, 6, 7, 9, 10}Expla
4 min read
How to Append Multiple Items to a List in Python Appending multiple items to a list in Python can be achieved using several methods, depending on whether you want to extend the list with individual elements or nested collections. Letâs explore the various approaches.Using extend method (Adding multiple items from iterable)The list.extend() method
2 min read
Python | Initializing multiple lists In real applications, we often have to work with multiple lists, and initialize them with empty lists hampers the readability of code. Hence a one-liner is required to perform this task in short so as to give a clear idea of the type and number of lists declared to be used. Method #1: Using loops We
4 min read
Python - Interleave multiple lists of same length When we interleave multiple lists, we mix the items from each list so that they alternate in a new sequence. This is often done when we have lists of the same length and want to combine them, with each item from each list appearing one after another. In this article, we will explore interleaving mul
2 min read
Python - List of tuples to multiple lists Converting a list of tuples into multiple lists involves separating the tuple elements into individual lists. This can be achieved using methods like zip(), list comprehensions or loops, each offering a simple and efficient way to extract and organize the data.Using zip()zip() function is a concise
3 min read
Python - Multiply Two Lists We are given two list we need to multiply given two list. For example, we are having two lists a = [1, 2, 3] b = [4, 5, 6] we need to multiply list so that the resultant output should be [4, 10, 18].Using a LoopWe can multiply two lists element-wise using a loop by iterating over both lists and mult
2 min read
Merge Multiple Lists into one List In this article, we are going to learn how to merge multiple lists into one list. extend() method is another simple way to merge lists in Python. It modifies the original list by appending the elements of another list to it. This method does not create a new list but instead adds elements to an exis
3 min read
Python List Add/Append Programs This article covers a wide range of methods for adding elements to a list, including:Basic addition techniques like append(), extend(), and insert().Appending multiple items, lists, tuples, dictionaries and objects.Performing list modifications such as adding at the beginning, middle or end.Advanced
3 min read