Online Tutorials & Training Materials | STechies.com
Register Login

How to Concatenate or Merge Two or Multiple lists in Python?

22 Nov 2019 1:02 pm || 0

Concatenate or merge two or multiple lists is a common operation of a programmer; in this tutorial, we will concatenate lists with and without duplicate elements.

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

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. 

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

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

Using Native Function 

To Concatenated to list we will traverse the second list using for loop and keep appending the first list with the element present in the second list and hence will have concatenated of two lists.

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]

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]

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]