Merge lists keeping duplicates in original list in python

In this post, we are going to understand how to Merge lists by keeping duplicates in the original list in Python. A list is a data structure in Python that contains data of different types, but some data in that list contains duplicate data, to manipulate data we need to merge those lists.

Merge two lists by maintaining the duplicate in the original list to get all the unique values from the second list that does not represent an original list.

Let us take an example to suppose we have these two lists with duplicate data. Merge lists with duplicates in Python. As we can understand with example the duplicate in original_list is maintained and unique values are merged to the original list from Second_list.

original_list = ['cat','bee','cat','dog','rat']

#the second list we have to merge in original list
second_list =  ['cat',1,2,3]

Merge two lists maintain duplicates in the original list in python output would be


merged_list = ['cat','bee','cat','dog','rat',1,2,3]

Using + operator and set()

The Set is in-built unordered collection datatype and does not contain duplicate data. It is used to perform different mathematics operations. In the code snippet, we are using the set to merge the second list.

How is it working

  • First, we are converting both lists into sets to get unique elements.
  • Then we are getting elements that exist in orglist but not exist in strlist.
  • By using the + operator we concatenate those elements in orglist and orglist_element_not_in_strlist and displaying output on the console
#Merge two lists maintain duplicates in the original list

orglist = ['cat','bee','cat','dog','rat']
lstnum = ['cat',1,2,3,]

#coverting both lists in set to get unique items only

set1 = set(orglist )
set2 = set(lstnum)

#here we are getting list of elements difference in both list

mylst_Items_NotIn_orglist= list(set2- set1)

#element different is merging in orglist

merged_list = orglist + mylst_Items_NotIn_orglist

print('merged list = \n',merged_list)


merged list = 
 ['cat', 'bee', 'cat', 'dog', 'rat', 1, 2, 3]

Using extend() method

The extend() method is a built-in method of list class. It takes an iterable (string, tuple, dictionary, list)and extends its end of the list. It modified original list object does not return a new list object. Let us understand with an example


#iterable can be (string, tuple, dictionary, list)

How it is working

  • First, we define a list result_list to perform the operation.
  • Now we are looping through all the elements of strlist, if the item does not exist in result_list, we are extending it in result_list .
  • Printing the output on the console.
#Merge two lists maintain duplicates in the original list
orglist = ['cat','bee','cat','dog','rat']
strlist = ['cat',1,2,3,]

#creating a result_list only perform operation

result_list = list(orglist) 
# Using extend function with result_list

result_list.extend(item for item in strlist if item not in result_list) 

#printing the output

print('merged list = \n',result_list)


merged list = 
 ['cat', 'bee', 'cat', 'dog', 'rat', 1, 2, 3]


In this post, we have learned how to Merge lists keeping duplicates in the original list in python with code example by using set with + operator and list.extend() method.