
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to find Duplicate sets in list of sets
When it is required to find duplicate sets in a list of sets, the ‘Counter’ and ‘frozenset’ are used.
Example
Below is a demonstration of the same
from collections import Counter my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, {1, 2, 6, 2}, {1, 4, 2}, {7, 8, 9}] print("The list is :") print(my_list) my_freq = Counter(frozenset(sub) for sub in my_list) my_result = [] for key, value in my_freq.items(): if value > 1 : my_result.append(key) print("The result is :") print(my_result)
Output
The list is : [{8, 1, 4, 6}, {8, 1, 4, 6}, {1, 2, 6}, {1, 2, 4}, {8, 9, 7}] The result is : [frozenset({8, 1, 4, 6})]
Explanation
A list of set values is defined and is displayed on the console.
It is iterated over using the ‘frozenset’ and ‘Counter’.
This gives the frequency of every value in the list.
This is assigned to a variable.
An empty list is created.
The elements of the variable are iterated over and if the frequency is greater than 1, this is appended to the empty list.
This is displayed as output on the console.
- Related Articles
- Python - Convert List of lists to List of Sets
- Python program to find common elements in three lists using sets
- Python program to find happiness by checking participation of elements into sets
- Program to find number of sets of k-non-overlapping line segments in Python
- How to split a Dataset into Train sets and Test sets in Python?
- Breaking a Set into a list of sets using Python
- Python Program to find out the number of sets greater than a given value
- Java Program to compare two sets
- Swift Program to Merge Two Sets
- How to create a dictionary of sets in Python?
- C# program to find common elements in three arrays using sets
- Java Program to Calculate union of two sets
- Program to find number of magic sets from a permutation of first n natural numbers in Python
- Python program to count the number of vowels using sets in a given string
- Java Program to Calculate the intersection of two sets

Advertisements