Removing Duplicates of a List of Sets in Python
Last Updated :
14 Feb, 2024
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])]
Similar Reads
Remove Duplicate Strings from a List in Python Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.Pythona = ["Learn", "Python", "With"
3 min read
Python - Remove Duplicates from a List Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists. Converting the list to a set removes all duplicates since sets
2 min read
Python | Remove duplicate tuples from list of tuples Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read
Python Program to Find Duplicate sets in list of sets Given a list of sets, the task is to write a Python program to find duplicate sets. Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result.
8 min read
Creating a List of Sets in Python Creating a list of sets in Python involves storing multiple sets within list structure. Each set can contain unique elements and the list can hold as many sets as needed. In this article, we will see how to create a list of set using different methods in Python.Using List ComprehensionList comprehen
2 min read