Open In App

Python Merge Two Lists Without Duplicates

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python coding, combining two lists without repeating elements can be a tricky task. It involves skillfully managing data structures to achieve a smooth and effective outcome. In this short guide, we’ll explore ways to merge lists in Python, making sure duplicates are handled seamlessly.

Python Merge Two Lists Without Duplicates

Below, are the methods to Python Merge Two Lists Without Duplicates in Python.

Python Merge Two Lists Without Duplicates Using Set Union

In this example, below Python code, two lists, `list1` and `list2`, are merged without duplicates by converting them into sets (`set1` and `set2`). The union of these sets is then obtained and converted back into a list (`ans`), resulting in a merged list free of duplicate elements.

Python3




list1 = [23, 45, 65, 31, 1, 89]
list2 = [67, 89, 23, 45, 8, 90]
 
# Converting the list into set
set1 = set(list1)
set2 = set(list2)
 
# find the union of the sets and converting resultant set to list
ans = list(set1.union(set2))
 
print("The resultant merged list is ")
print(ans)


Output

The resultant merged list is 
[65, 1, 67, 8, 45, 23, 89, 90, 31]



Python Merge Two Lists Without Duplicates Using For Loop

In this example, below Python code snippet, two lists, `list1` and `list2`, are merged without duplicates using a manual approach. The code iterates through each element in both lists, checking for duplicates and appending unique elements to the resultant list `ans`.

Python3




list1 = [23, 45, 65, 31, 1, 89]
list2 = [67, 89, 23, 45, 8, 90]
 
# Resultant list
ans = []
 
# Traversing the list item one by one
for data in list1:
    if data not in ans:
        ans.append(data)
         
for data in list2:
    if data not in ans:
        ans.append(data)
 
print("The resultant merged list is ")
print(ans)


Output

The resultant merged list is 
[23, 45, 65, 31, 1, 89, 67, 8, 90]



Python Merge Two Lists Without Duplicates Using list() Method

In this example, below Python code merges two lists, `list1` and `list2`, without duplicates. It uses the set union operation to combine the elements of both lists, and then converts the resulting set back into a list (`ans`).

Python3




list1 = [23, 45, 65, 31, 1, 89]
list2 = [67, 89, 23, 45, 8, 90]
 
# Resultant list
ans = list(set(list1 + list2))
 
print("The resultant merged list is ")
print(ans)


Output

The resultant merged list is 
[65, 1, 67, 8, 45, 23, 89, 90, 31]



Python Merge Two Lists Without Duplicates Using list comprehension

In this example, below Python code efficiently merges two lists, `list1` and `list2`, without duplicates. It creates the resultant list (`ans`) by combining `list1` with elements from `list2` that are not already present in `list1`.

Python3




list1 = [23, 45, 65, 31, 1, 89]
list2 = [67, 89, 23, 45, 8, 90]
 
# Resultant list
ans = list1 + [data for data in list2 if data not in list1]
 
print("The resultant merged list is ")
print(ans)


Output

The resultant merged list is 
[23, 45, 65, 31, 1, 89, 67, 8, 90]



Conclusion

There are various ways like list comprehension, Loops, set intersection method, or using list(set()) to merge two list without duplicates. developers have a range of efficient options. The choice of method depends on specific requirements and preferences, allowing for flexibility in merging of two list without duplicates.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads