How to Merge multiple lists in Python

In this article, we will learn How to Merge multiple lists in Python or How to join or concatenate lists with code examples.It includes +,append(),extend() and many more.Let us start with each method & with the help of examples, we will cover them one by one.

1. Python merge lists without duplicates


In this example, we will learn how to merge or join multiple lists without duplicates. We have used Python Set datatype that does not allow duplicate.

animal_list = ['cat','bee','cat','rat']
 
animal_list2 = [1,2,3,'mouse']

number_list = [48,67,63]


unique_list = list(set(animal_list + animal_list2+number_list ))

print(unique_list)

Output

[1, 2, 3, 'mouse', 67, 'rat', 'cat', 48, 'bee', 63]

2. How to merge multiple lists in Python Using + operator


The + operator is used to concatenate a string and we can use it to concatenate two lists or multiple lists also. Let us understand with an example In Python merges two lists using + operator, it returns a new list object after merging.

We have used the + operator to merge multiple python lists.

Python Program to merge two or multiple list using + operator

animal_list = ['cat','bee','cat','cat','rat']

animal_list1 = [1,2,3,'mouse']

mergelist = animal_list+animal_list1

print('The merged two list\n  =',mergelist)

merge_mullist = animal_list+animal_list1+number_list

print('The mutiple merged list\n  =',merge_mullist )

Output

The merged two list
  = ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse']
['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse', 48, 67, 63]

3. How to Merge multiple lists in Python Using extend()


To merge lists in python, we make use of the list built-in extend() method. The element of the second list extends at end of the existing list. It does not return a new list only extends the first list. Finally, we are printing the merged python lists.

#Python merge lists using extend() method

animal_list = ['cat','bee','cat','cat','rat']

animal_list1 = [1,2,3,'mouse']

animal_list.extend(animal_list1)

print('The two merged list\n  =',animal_list)

Output

The two merged list
  = ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse']

4. How to Merge multiple lists in Python Using append() method


The list. append() method of list class appends a list at end of the first list. It appends the second whole list. It does not append each element of the list in sequence to the existing list. Instead, append a whole list at end of another list. As we can see the whole animal_list1 list is appended at the end of animal_list

#Program to merge append() method

animal_list = ['cat','bee','cat','cat','rat']

animal_list1 = [1,2,3,'mouse']

animal_list.append(animal_list1)

print('The two merged list\n  =',animal_list)

Output

The two merged list
  = ['cat', 'bee', 'cat', 'cat', 'rat', [1, 2, 3, 'mouse']]

5. Merge list Using append() with for-loop


We can loop over the second list to append all its elements at end of another list. In the below result output as we can see each element got appended.

#Program to python merge lists using append() method 

animal_list = ['cat','bee','cat','cat','rat']

animal_list1 = [1,2,3,'mouse']

for element in animal_list1  : 
 animal_list.append(element)

print('The two merged list\n  =',animal_list)

Output

The two merged list
  = ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse']

6. Using list comprehension to merge lists


The list comprehension can be used to merge Mutiple lists in python code with a shortcode and one-line code. Let us understand with an example. It returns the new list after merging two lists.

#Program to python merge lists Using list comprehension 

animal_list = ['cat','bee','cat','cat','rat']

animal_list1 = [1,2,3,'mouse']

merged_list = [item_y for item_x in [animal_list, animal_list1] for item_y in item_x]

print('The two merged list\n  =',merged_list)

Output

The two merged list
  = ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse']

7. Using itertools.chain()


The chain() is a function in itertools a module that takes a sequence of iterable(tuple, string, list, dictionary) and joins them, and returns a single iterable as a result, therefore we are using it to merge lists in python.

Here is a code Example:

#Program to merege two list in python

import itertools 

animal_list = ['cat','bee','cat','cat','rat']

animal_list1 = [1,2,3,'mouse']

merged_list = list(itertools.chain(animal_list, animal_list1))

print('The two merged list\n  =',merged_list)

Output

The two merged list
  = ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse']

8. Pythom Merge multiple lists Using *(asterisk)


This unpacking *operator used in Python 3.6 and above, with an iterable(list, string, tuple, dictionary) operand, we can merge multiple lists using the *operator

Here is the code Example:

#Program to merge multiple list in python

animal_list = ['cat','bee','cat','cat','rat']

animal_list1 = [1,2,3]

num_list = [89,45,67,87]

merged_list = [*animal_list, *animal_list1,*num_list] 

print('The two merged list\n  =',merged_list)

Output

The two merged list
  = ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 89, 45, 67, 87]

Conclusion :

We hope you could learn different 7 ways to Merge or join lists in Python. This will help you to merge multiple lists in python. The + operator or *(asterisk ) operator can be used for multiple lists in python.