Open In App

Python – Append Multiple Lists at Once

Last Updated : 14 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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].

Using itertools.chain()

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)

Output
[1, 2, 3, 4, 5, 6]

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)

Output
[1, 2, 3, 4, 5, 6]

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)

Output
[1, 2, 3, 4, 5, 6]

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)

Output
[1, 2, 3, 4, 5, 6]

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)

Output
[1, 3, 5, 2, 4, 6]

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)

Output
[1, 2, 3, 4, 5, 6]


Similar Reads