Signup/Sign In

How to Add two Lists in Python?

Posted in Programming   LAST UPDATED: MARCH 15, 2021

    Lists in Python are data structures that are mutable, meaning, the elements inside the list can be changed. In this tutorial, we will learn how to add or we can say concatenate two lists in python. And we wouldn't just cover a single way of doing so, but we will try to share 10 different ways of doing so with simple code examples.

    1. Using the + operator

    This simply concatenates the first list with the second or the left-hand operand with the right-hand operand(works with strings too). It retains the order in which the list was defined. This is one of the easiest ways to add elements of one list to another list. The data type of the data elements in both the list has no effect.

    Time for an example:

    list_one = [1,'a',3]
    list_two = [4,'b',6]
    print(list_one + list_two)
    list_one += list_two
    print(list_one)

    Output:

    [1, 'a', 3, 4, 'b', 6]
    [1, 'a', 3, 4, 'b', 6]

    Suppose you wish to remove all the duplicates present in both the lists which are supposed to be concatenated. This can be done using the + operator and the set function in conjunction. Using the set function creates a set data structure which cannot have duplicate values, hence it automatically removes the duplicate values, and then we can comfortably convert it back to a list using the list function.

    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    print(list(set(list_one + list_two)))

    Output:

    [0, 1, 2, 3, 4, 6, 7]

    2. Using itertools

    The itertools is a module that implements many building blocks in Python. Adding two lists can be done by using the chain() function of the itertools module which makes an iterator, which returns all the elements of the first iterable followed by all the elements of the next iterable, thereby allowing you to provide as many iterable objects as you want.

    In the code below, we have used the chain() function to iterate over two lists, and we have simply used the list function to create a new list with all those elements. We can also directly print the values too.

    import itertools
    
    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    list_three = list([item for item in itertools.chain(list_one, list_two)])
    print(list_three)

    Output:

    [1, 2, 4, 4, 7, 3, 6, 7, 2, 0]

    Suppose you have a single data sequence which has multiple iterables (like lists, tuples or generator) inside it and you want to create a single list using elements of al those iterables, the from_iterable function can be used in conjunction with itertools.chain() function as we have shown in the example below. In the example below, we have a list which has a list and a tuple inside it as data elements. We have used the from_iterable function along with the chain() function to create a single iterable(in our case, list) with all the elements from both list and tuple:

    multiple_iterables = [['a','b','c'], (1,2,3,5,0)]
    list_three = list([i for i in itertools.chain.from_iterable(multiple_iterables)])
    
    print(list_three)

    Output:

    ['a', 'b', 'c', 1, 2, 3, 5, 0]

    3. Using the extend method

    The extend method can be used to concatenate lists as this method can be used to add elements of one list to another list. More about the extend method here.

    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    # creating third list to contain data of list one and two
    concatenated_list = []
    concatenated_list.extend(list_two)
    concatenated_list.extend(list_one)
    print(concatenated_list)

    Output:

    [3, 6, 7, 2, 0, 1, 2, 4, 4, 7]

    4. Using the append method

    The lists to be concatenated have to be passed separately every time to the append method. The execution time of this method is high in comparison to other methods. More about the append method can be found here.

    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    concatenated_list = []
    concatenated_list.append(list_one)
    concatenated_list.append(list_two)
    print(concatenated_list)

    Output:

    [[1, 2, 4, 4, 7], [3, 6, 7, 2, 0]]

    5. Using unpacking technique (* Operator)

    If you have Python version >= 3.5, you could use the unpacking method using the * operator. Using the * operator with any iterable returns its elements. This functionality was defined for version 3.5 and hasn't been taken back. It will give a SyntaxError if this functionality is not supported by your Python version.

    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    # elements from both lists
    concatenated_list = [*list_one, *list_two]
    print(concatenated_list)

    Output:

    [1, 2, 4, 4, 7, 3, 6, 7, 2, 0]

    Suppose I have a list of lists, and I wish to add these elements into a single list, how do I do it? Yes, we saw how to do it using the itertools module above, using its chain() function with the from_iterable function but we can also do this using the itertools.chain with unpacking operation, like in the example below:

    list_one = [[1,2,3], ['a','b','Studytonight'], [7,8,0]]
    list(itertools.chain(*list_one))

    Output:

    [1, 2, 3, 'a', 'b', 'Studytonight', 7, 8, 0]

    6. Using the yield statement in a method

    The yield statement can be used as the return statement, but the yield statement gives back a generator. This method comes in handy when the user knows that the method would return a huge dataset which might have to be read just once, as the yield statement can maintain state. When the yield method is called, the code written within the method doesn't run. It only returns a generator object.

    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    
    def merge(l1, l2):
        yield from l1
        yield from l2
    list(merge(list_one, list_two))

    Output:

    [1, 2, 4, 4, 7, 3, 6, 7, 2, 0]

    7. Merge method from heapq library

    The merge() method of the heapq library returns the concatenated elements in the same order as that of the original lists provided as arguments.

    from heapq import merge
    
    list_one = [1,2,4,4,7]
    list_two = [8,9,1,0]
    print(list(merge(list_one, list_two)))

    Output:

    [1, 2, 4, 4, 7, 8, 9, 1, 0]

    Just using the merge function gives a generator which might not be what you need. Below is an example demonstrating the same,

    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    print(merge(list_one, list_two))

    Output:

    <generator object merge at 0x000001FCD34EE750>

    8. Using the add method from the operator library

    The add method present in the operator library can be used to concatenate two lists. The operator module has efficient functions for comparing objects, mathematical operations on objects, logical operations on objects and much more. The add method in this module corresponds to a simple expression x+y.

    import operator
    
    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    print(operator.add(list_one, list_two))

    Output:

    [1, 2, 4, 4, 7, 3, 6, 7, 2, 0]

    9. Using the add dunder method

    Dunder basically means double underscore method_name double underscore. Since this seems too clumsy to say, it has been shorthanded as dunder, which means double underscores before and after the method name. These methods are also known as special methods. Usually, it is not suggested to use dunder methods. These dunder methods can be invoked after the instance of a class has been created.

    from functools import reduce
    
    list_one = [[1,2,3], ['a','b','Studytonight'], [7,8,0]]
    reduce(list.__add__, list_one)

    Output:

    [1, 2, 3, 'a', 'b', 'Studytonight', 7, 8, 0]

    10. Using list comprehension

    List comprehensions use iterables to create new lists. Their return type is the new list which is generated by specifying a condition, iterating over a list or any other data structure.

    list_one = [1,2,4,4,7]
    list_two = [3,6,7,2,0]
    concatenated_list = []
    concatenated_list = [item for list_ in [list_one, list_two] for item in list_]
    print(concatenated_list)

    Output:

    [1, 2, 4, 4, 7, 3, 6, 7, 2, 0]

    Conclusion

    In today's post, we saw various methods to concatenate two lists. These methods can be used depending on the requirement in hand.

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:PythonPython ListProgramming
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS