Python : Join / Merge Two or More Lists

Basic Concepts/Terminologies

In this article, we will discuss different ways to join two or more lists in python. Before going to the actual let us understand about lists in python.

List

The list is one of the data structures in python that is used to store multiple items in a single variable. The best thing about the list is that we can store the value of different data types in a single list i.e. we can store string, int, and float values in the same list.

l=[1,2,"list",3.3,"Raj",4]
print(type(l))

Output

<class 'list'>

In this example, we see how we can easily store different variables in the same list.

Ways to join two or more lists in python

  • Method 1-Using the “+” operator

Here the role of the “+” operator is the same as in maths. The “+” operator can easily join/merge two or more lists like we add two or more numbers in maths. In python, we called this concept concatenation. Let us see this concept with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
new_l=l1+l2
print(new_l)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

Here we see that how we easily concatenate our lists using the “+” operator. The second thing to notice here is that the list that comes before the “+” operator has also come before another list in the new list.

  • Method 2-Using for loop

This is also one of the methods to merge two or more lists in python. In this method, we iterate through all the elements in one list and append these elements in the second list. Fore appending element in the second list we can use the append() method which is an inbuilt method in a python list. Let us see this method with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
for item in l1:
    l2.append(item)
print(l2)

Output

[8, 1.1, 'list2', 1, 2, 'list1', 3.3, 'Raj', 4]
  • Method 3-Using list.extend()

extend() method is used to add all the elements of an iterable i.e. list, tuple, etc at the end of the list. The best thing about this method is that we don’t need to use it for loops. we simply pass our iterable as an argument in extend() function and our task will be done. This is an in-place method that means it made changes in an existing list instead of creating a new list.

syntax: list.extend(anotherList)

Let us see this method with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l1.extend(l2)
print(l1)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']
  • Method 4-Using list comprehensions

Before understanding this method let us take a quick look at what list comprehension is.

List comprehensions

List comprehension is a way of creating a new list from the existing list. It helps in solving the problem related to list in one line.

Let us see how to join two lists using list comprehension with the help of an example.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l = [y for x in [l1, l2] for y in x]
print(l)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

Here we how we easily join our two lists using list comprehension.

  • Method 5- Using itertools.chain()

Before understanding this method let us see what an itertools is.

itertools

It is a module in python that provides methods to work flexibly with different iterables in python. To use this module we have to import this using the import keyword in our program.

itertools.chain()

This method takes multiple iterables at a one-time group them and produces a single iterable as output.

syntax-itertools.chain(*iterables)

Here * indicates that we can pass multiple iterable as a parameter in this function.

Let us see this concept with the help of an example.

import itertools
l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l=itertools.chain(l1, l2)
merge_list=list(l)
print(l)
print(merge_list)

Output

<itertools.chain object at 0x000002A9837445F8>
[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

Explanation

This method returns a chain object as we see in the output. We convert this chain object to a list to get our desired output.

  • Method 6- Using unpacking or “*” operator

As the name suggests unpacking or “*:” is used to unpack the content of any iterables object. Hence we can unpack the content of two or more lists and then merge them to form a new list.

Note: This method works only in python3.6+ versions.

l1=[1,2,"list1",3.3,"Raj",4]
l2=[8,1.1,"list2"]
l = [*l1, *l2]
print(l)

Output

[1, 2, 'list1', 3.3, 'Raj', 4, 8, 1.1, 'list2']

So these are the methods to append two or more lists in python.