Python Append Multiple Lists at Once (original) (raw)

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:

Other methods that we can use to append multiple lists at once are:

Table of Content

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:

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:

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)

`