Skip to content

Python merge multiple lists

  • by

Using + Operator you can merge multiple lists in Python. In Python, concatenation can be performed on numbers, strings, and elements of a list.

Python merges multiple lists

A simple example code adds a list behind another list with retains the order of list elements and contains duplicate elements.

list1 = [2, 3, 4, 2, 2]
list2 = [4, 5, 6, 7]
list3 = [1, 5, 33, 2]

# merge lists using + Operator
res = list1 + list2 + list3

# Print output
print('Merged List: ', res)

Output:

Python merges multiple lists

The extend() function is also used to concatenate multiple lists.

# concatenate lists using extend()
list1.extend(list2)
list1.extend(list3)

(*) Operator works the same as (+) operator, with this we can concatenate to or more lists it works with Python 3.6+ versions.

# merge lists using * Operator
newlist = [*list1, *list2, *list3]

Using itertools.chain()

import itertools
ab = itertools.chain(['it'], ['was'], ['annoying'])
list(ab)

Do comment if you have any doubts or suggestions for this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading