Open In App

Removing Duplicates of a List of Sets in Python

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

Dealing with sets in Python allows for efficient handling of unique elements, but when working with a list of sets, you may encounter scenarios where duplicates need to be removed. In this article, we will see how we can remove duplicates of a list of sets in Python.

Remove Duplicates of a List of Sets in Python

Below are some the ways by which we can remove duplicates of a list of sets in Python:

Using a loop and not Keyword

In this example, a loop iterates through the list of sets (`list_of_sets`), and using the `not in` condition, it appends each set to the `unique_list` only if it is not already present, effectively removing duplicates.

Python




list_of_sets = [{1, 2, 3}, {2, 3, 4}, {2, 1, 3}]
 
# Using a loop and not in to remove duplicates
unique_list = []
 
# iterating list
for s in list_of_sets:
    if s not in unique_list:
        unique_list.append(s)
 
print(unique_list)


Output

[set([1, 2, 3]), set([2, 3, 4])]

Using a Set and List Comprehension

In this example, a set and list comprehension is used to create a set of frozensets, ensuring unique sets while maintaining order. A list comprehension is then employed to convert the frozensets back to sets, producing the `unique_list` with duplicates removed.

Python




list_of_sets = [{1, 2, 3}, {2, 3, 4}, {1, 2, 3}]
 
# Using a set and list comprehension to remove duplicates
unique_list = [set(s) for s in {frozenset(s) for s in list_of_sets}]
 
print(unique_list)


Output

[set([1, 2, 3]), set([2, 3, 4])]

Using frozenset() Method

In this example, a loop iterates through a set comprehension `{frozenset(s) for s in list_of_sets}`, creating a set of unique frozensets to eliminate duplicates. For each frozenset in this set, the loop appends the corresponding set to `unique_list`, resulting in a list with duplicates removed.

Python




list_of_sets = [{1, 2, 3}, {2, 3, 4}, {1, 2, 3}]
 
# Using a set and list comprehension to remove duplicates
unique_list = []
 
for s in {frozenset(s) for s in list_of_sets}:
    unique_list.append(set(s))
print(unique_list)


Output

[set([1, 2, 3]), set([2, 3, 4])]



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads