Removing Duplicates of a List of Sets in Python (original) (raw)
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 | Removing duplicates from tuple Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() 4 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
- Python - Remove duplicate words from Strings in List Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The 6 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
- Python | Remove duplicates from nested list The task of removing duplicates many times in the recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let's discuss certain ways in which this task can be achieved. Method #1 : Using sorted() + set()Â Th 5 min read
- Python | Remove duplicate lists in tuples (Preserving Order) Sometimes, while working with records, we can have a problem in which we need to remove duplicate records. This kind of problem is common in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() In this method, we test fo 7 min read
- Python | Remove duplicates based on Kth element tuple list Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop Th 8 min read