Register Login

Concatenate or Merge Two or Multiple lists in Python?

Updated Mar 25, 2020

Combining strings or characters in programming is called concatenation. In Python, concatenation can be performed on numbers, strings and elements of a list. For example, you can add a string “Hey” and another string “there!” to form a new string “Hey there!”. You can also add two numbers such as 2 and 5 to get a result 7. The most common form of concatenation is by using the + operator.

Concatenating or merging two or multiple lists is a common operation of a programmer. In this tutorial, we will concatenate lists with and without duplicate elements.

1) Using + Operator 

Its a very common operator to concatenate lists it simply adds list behind another list.

Example:

# Python program to merge lists
# Using + Operator

# Initializing lists
list1 = [2,3,4,2,2]
list2 = [4,5,6,7,34,56]
list3 = [1,5,33,2,34,46]

# merge lists using + Operator
newlist = list1 + list2 + list3

# Print output
print('Merged List: ',newlist)

Output:

Merged List: [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56, 1, 5, 33, 2, 34, 46]

Note:

  • It retains the order of list elements
  • Contain duplicate elements
  • Concatenated two or more lists

Explanation

Here, the three lists are combined together using + operator. This is used because the + operator is one of the easiest ways to combine multiple lists into a single one.   

2) Using extend() Function 

extend() function is also used to concatenate lists, it simply adds the whole list at the end of the first list.

Example:

# Python program to concatenate lists
# Using extend function 

# Initializing lists
list1 = [2,3,4,2,2]
list2 = [4,5,6,7,34,56]
list3 = [4,67,2,2,4,66]

# concatenate lists using extend()
list1.extend(list2)
list1.extend(list3)

# Print output
print('Concatenated List: ',list1)

Output:

Concatenated List: [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56, 4, 67, 2, 2, 4, 66]

Note: 

  • It retains the order of list elements
  • Contain duplicate elements
  • It only concatenates two lists; if you want to concatenate more then two lists, then you need to use extend() function multiple times, as shown in the above example. 

Explanation

In this program, the extend() method is used for concatenating all three lists together. You can see that the extend() method is used two times for merging the second and third list with the first list. As mentioned earlier, the method cannot add more than two lists. Thus it is used two times for adding the first list with the second and third list.          

3) Using (*) Operator

(*) Operator works the same as (+) operator, with this we can concatenate to or more list it works with Python 3.6+ versions.

Example:

# Python program to merge lists
# Using * Operator

# Initializing lists
list1 = [2,3,4,2,2]
list2 = [4,5,6,7,34,56]
list3 = [1,5,33,2,34,46]

# merge lists using * Operator
newlist = [*list1, *list2, *list3]

# Print output
print(Concatenated List: ',newlist)

Output:

Concatenated List: [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56, 1, 5, 33, 2, 34, 46]

Note:

  • It retains the order of list elements
  • Contain duplicate elements
  • Concatenated two or more lists

Explanation

Here the (*) operator is used for merging the three lists together while maintaining the order of the elements. The (*) operator is utilized here as multiple lists can be easily added using it.     

4) Using itertools.chain()

Example:

# Python program to concatenate lists
# Using itertools

import itertools

# Initializing lists
list1 = [2,3,4,2,2]
list2 = [4,5,6,7,34,56]
list3 = [1,5,33,2,34,46]

# Concatenate lists using itertools
newlist = list(itertools.chain(list1, list2, list3)) 

# Print output
print('Concatenated List: ',newlist)

Output

Concatenated List:  [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56, 1, 5, 33, 2, 34, 46]

Note: 

  • Works with numpy array
  • It retains the order of list element
  • Contain duplicate elements
  • Concatenated tow or more lists

Explanation

In the first line, the import keyword is used to load the itertools module. The variables list1, list2 and list3 are assigned values [2,3,4,2,2], [4,5,6,7,34,56] and [1,5,33,2,34,46] respectively. Then, a newlist variable is assigned the concatenated values of the three lists.

In the next line, the itertools.chain() method is passed the arguments of list1, list2 and list3. This method takes multiple arguments and returns a single sequence of items. So, the chain() method concatenates all three lists. The result of the method call is converted into a list using the list() method.

The newlist variable is printed in the last line of the program using the code print('Concatenated List: ',newlist)

5) Using Native Function 

To concatenate two lists, we will first traverse the second list using a for loop. We will keep appending the elements of this list to the first list. As a result, the two lists will be added.  

Example:

# Python program to concatenate lists
# Using for loop and append function

# Initializing lists
list1 = [2,3,4,2,2]
list2 = [4,5,6,7,34,56]

# merge list using for loop and append function
for x in list2 : 
    list1.append(x)  

# Print output
print('Concatenate List: ',list1)

Output:

Concatenate List: [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56]

Explanation

Let us try to understand this code. Here, a for loop is used for adding the two lists using the append() method. This process is used as the append() method adds a single element to the end of a list. Thus, each element of the list2 is added to list1, one by one using the for loop.

Single Line Code Example:

# Python program to concatenate lists

# Initializing lists
list1 = [2,3,4,2,2]
list2 = [4,5,6,7,34,56]
list3 = [1,5,33,2,34,46]

# Concatenate lists
newlist = [y for x in [list1, list2, list3] for y in x]

# Print output
print(Concatenated List: ',newlist)

Output:

Concatenated List: [2, 3, 4, 2, 2, 4, 5, 6, 7, 34, 56, 1, 5, 33, 2, 34, 46]

Explanation

We can observe that the shorthand version of a for loop is used here for concatenation. This list comprehension technique is used to avoid writing multiple inner loops, as there are three lists that have to be added. As a result, the process is fast and efficient.

Merge or Concatenate Lists Without Duplicates

From all the above examples, we can see that the final concatenate list has duplicate elements, if we want to remove duplicate from the list then we need to convert the list to “set” and then convert back to “list.”

Example:

# Python program to Concatenate lists
# Using + Operator

# Initializing lists
list1 = [2,3,4,2,2]
list2 = [4,5,6,7,34,56]
list3 = [1,5,33,2,34,46]

# Concatenate lists using + Operator
newlist = list1 + list2 + list3

# Convert final list to set() and then set to list()
newlist = list(set(newlist))

# Print output
print('Concatenated List Without Duplicate: ',newlist)

Output:

Concatenated List Without Duplicate: [1, 2, 3, 4, 5, 6, 7, 34, 33, 46, 56]

Explanation

You can see that the main aim of the program is used to merge three lists that do not have duplicate elements. So, after concatenating the lists using a + operator, the resultant list is passed to the in-built set() method. As Python sets do not have any duplicate elements, this removes all the duplicates from the concatenated list. As we require a list, this set is converted into a list using the list() method.     

Conclusion

Concatenation is one of the easiest ways to combine list elements or strings while codding in Python. But while using the chains() method, import the itertools module using the import statement. Additionally, you can also use methods such as join() to concatenate two strings.


×