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 contain 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)
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 list it works with Python 3.6+ versions.
# merge lists using * Operator newlist = [*list1, *list2, *list3]
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)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.