In this article we will discuss different ways to Merge / Join two or more lists.

Suppose we have two lists i.e.

# List of strings
list1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list2 = [10, 2, 45, 3, 5, 7, 8, 10]

Now lets discuss how to merge contents of these list into a single list i.e.
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

Merging two lists using + operator

We can use + operator to merge two lists i.e.

# Merge two lists
finalList = list1 + list2

It will return a new concatenated lists i.e.
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

list1 and list2 will remain same.

Merging two lists using list.extend()

If we want to extend any existing list by concatenating the contents of any other lists to it. Then we should use lists’ extend() method.

list.extend(anotherList)

list.extend() makes a list longer by appending the elements of another list at the end.

Suppose we have two lists i.e.

# List of strings
list1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list2 = [10, 2, 45, 3, 5, 7, 8, 10]

Let’s extend list1 by adding the contents of list2 at the end of list1 i.e.
# Makes list1 longer by appending the elements of list2 at the end.
list1.extend(list2)

Now contents of list1 will be extended i.e.
extended list1  :  ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

Merging multiple lists using + operator

Suppose we have 3 lists i.e.

list1 = ["This" , "is", "a", "sample", "program"]
list2 = [10, 2, 45, 3, 5, 7, 8, 10]
list3 = [11, 12, 13]

Now let’s merge all the three lists into a single list i.e.
# Merge 3 lists into a single list    
finalList = list1 + list2 + list3

New list’s contents will be,
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10, 11, 12, 13]

Complete example is as follows,
def main():
    
    # List of strings
    list1 = ["This" , "is", "a", "sample", "program"]
    
    # List of ints
    list2 = [10, 2, 45, 3, 5, 7, 8, 10]
    
    print("list1 : ", list1)
    print("list2 : ", list2)
    
    '''
        Merge / Join two lists using + operator
    '''
    # Merge two lists
    finalList = list1 + list2
    
    print("Merged List : " , finalList)
    
    '''
        Merge / Join two lists using list.extend()
    '''
    # Makes list1 longer by appending the elements of list2 at the end.
    list1.extend(list2)
    
    print("extended list1  : " , list1)
    
 
    '''
        Merge / Join multiple lists using + operator
    '''
    
    list1 = ["This" , "is", "a", "sample", "program"]
    list2 = [10, 2, 45, 3, 5, 7, 8, 10]
    list3 = [11, 12, 13]
    
    # Merge 3 lists into a single list    
    finalList = list1 + list2 + list3
    
    print("Merged List : " , finalList)
    
        
if __name__ == '__main__':
    main()

Output:
list1 :  ['This', 'is', 'a', 'sample', 'program']
list2 :  [10, 2, 45, 3, 5, 7, 8, 10]
Merged List :  ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]
extended list1  :  ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]
Merged List :  ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10, 11, 12, 13]

 

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.