7 Ways to Merge multiples Python lists

Python List

In this article, we are going to learn different 7 Ways to Merge multiples Python 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 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 merged.

1.1 Python merge two lists using + operator

#Program to Python merge two lists

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)

Output

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

1.2 Python Merge multiple lists by using + operator

The + operator can be used to merged multiple lists and create a single list.In the below example, we are merging multiple python lists using the + operator.

#Program to merge/join multiple lists in python'

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

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

number_list = [48,67,63]

mergelist = animal_list+animal_list1+number_list

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

Output

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

2. Python merge lists Using extend() method


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 extend the first list.Finally we are printing the merge 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']

3.Merge lists Using append() method


The 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 and can be used only when you want to append full list. As we can see the whole animal_list1 list is appended.

#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']]

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


We can loop over the second list to append all its element,as we can see with below example that 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']

5. Using list comprehension to merge lists


The list comprehension can be used to merge two lists in python code with short code. Let us understand with an example. It returns the new list after merge of two list.

#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']

6. 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 list 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']

7. Merge multiple lists Using *(asterisk)


This unpacking *operator used in Python 3.6 and above, with 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.