The Wayback Machine - https://web.archive.org/web/20240926222934/https://www.geeksforgeeks.org/removing-duplicates-of-a-list-of-sets-in-python/
Open In App

Removing Duplicates of a List of Sets in Python

Last Updated : 14 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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. Input : test_list = [{4, 5, 6, 9}, {6, 4, 1, 5}, {
8 min read
Python Program For Removing All Occurrences Of Duplicates From A Sorted Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. Examples: Input: 23->28->28->35->49->49->53->53 Output: 23->35 Input: 11->11->11->11->75->75 Output: empty List Note that this is different from Remove Duplica
3 min read
Python Program For Removing Duplicates From A Sorted Linked List
Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60. Recommended: Please solve it on "PRACTICE"
7 min read
Python Program For Removing Duplicates From An Unsorted Linked List
Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43. Recommended: Please solve it on "PRACTICE" first, before moving
4 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() + tuple() This is the most straight forward way t
4 min read
Python - Convert List of lists to list of Sets
Given a list containing lists, the task is to write a Python Program to convert it to a list containing sets. Examples: Input : [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]Output : [{1, 2}, {1, 2, 3}, {2}, {0}]Input : [[4, 4], [5, 5, 5], [1, 2, 3]]Output : [{4}, {5}, {1, 2, 3}]Python Convert List of lists to list of SetsBelow are the ways by which we
4 min read
Python | Merging duplicates to list of list
Sometimes, we need to perform the conventional task of grouping some like elements into a separate list and thus forming a list of lists. This can also help in counting and also get the sorted order of elements. Let's discuss certain ways in which this can be done. Method #1: Using collections.Counter() This particular function can prove to be quit
6 min read
Breaking a Set into a List of Sets using Python
Given a set of elements p, the task is to write a Python program to convert the whole set into a list of Python Sets. Example: Input : p = {'sky', 'is', 'blue'} Output : [{'blue'}, {'is'}, {'sky'}] Explanation : Here each element of the set is converted into a individual set. Input : p = {10, 20, 30, 40} Output : [{10}, {20}, {30}, {40}] Breaking a
2 min read
Find the Union on List of Sets in Python
The union of sets involves combining distinct elements from multiple sets into a single set, eliminating duplicates. In this article, we will study how to find the union on a list of sets in Python. Find the Union on a List of Sets in PythonBelow are some of the ways by which we can find the union on a list of sets in Python: Using union() method i
4 min read
Creating a List of Sets in Python
We are given a task to create a list of sets in Python using different approaches. In this article, we will see how to create a list of set using different methods in Python. Example : Input : [] Output: [{0, 1, 2}, {3, 4, 5}, {8, 6, 7}, {9, 10, 11}] Explanation: We have given an list and here we are creating list of sets.Creating a List of Sets in
2 min read
Python Remove Duplicates from a List
The job is simple. We need to take a list, with duplicate elements in it and generate another list that only contains the element without the duplicates in them. Examples: Input : [2, 4, 10, 20, 5, 2, 20, 4] Output : [2, 4, 10, 20, 5] Input : [28, 42, 28, 16, 90, 42, 42, 28] Output : [28, 42, 16, 90] We can use not in on list to find out the duplic
3 min read
Python | Program to print duplicates from a list of integers
Given a list of integers with duplicate elements in it. The task is to generate another list, which contains only the duplicate elements. In simple words, the new list should contain elements that appear as more than one. Examples: Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20] Output : output_list = [20, 30, -20, 60]Input :
5 min read
Python | Program to count duplicates in a list of tuples
Given a list of tuples, write a Python program to check if an element of the list has duplicates. If duplicates exist, print the number of occurrences of each duplicate tuple, otherwise print "No Duplicates". Examples: Input : [('a', 'e'), ('b', 'x'), ('b', 'x'), ('a', 'e'), ('b', 'x')] Output : ('a', 'e') - 2 ('b', 'x') - 3 Input : [(0, 5), (6, 9)
6 min read
Python | Remove consecutive duplicates from list
In Python, we generally wish to remove the duplicate elements, but sometimes for several specific usecases, we require to have remove just the elements repeated in succession. This is a quite easy task and having a shorthand for it can be useful. Let's discuss certain ways in which this task can be performed. Method #1 : Using groupby() + list comp
4 min read
Python | Remove all duplicates and permutations in nested list
Given a nested list, the task is to remove all duplicates and permutations in that nested list. Input: [[-11, 0, 11], [-11, 11, 0], [-11, 0, 11], [-11, 2, -11], [-11, 2, -11], [-11, -11, 2]] Output: {(-11, 0, 11), (-11, -11, 2)} Input: [[-1, 5, 3], [3, 5, 0], [-1, 5, 3], [1, 3, 5], [-1, 3, 5], [5, -1, 3]] Output: {(1, 3, 5), (0, 3, 5), (-1, 3, 5)}
4 min read
Python | Combine two lists by maintaining duplicates in first list
Given two lists, the task is to combine two lists and removing duplicates, without removing duplicates in original list. Example: Input : list_1 = [11, 22, 22, 15] list_2 = [22, 15, 77, 9] Output : OutList = [11, 22, 22, 15, 77, 9] Code #1: using extend C/C++ Code # Python code to combine two lists # and removing duplicates, without # removing dupl
6 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() This particular problem can be solved using the above
5 min read
Python | Sort given list by frequency and remove duplicates
Problems associated with sorting and removal of duplicates is quite common in development domain and general coding as well. The sorting by frequency has been discussed, but sometimes, we even wish to remove the duplicates without using more LOC's and in a shorter way. Let's discuss certain ways in which this can be done. Method #1 : Using count()
5 min read
Python | Consecutive duplicates all elements deletion in list
Sometimes, while working with Python list, a problem can occur to filter list to remove duplicates. The solution to this has been discussed before. But sometimes, we may have a problem in which we need to delete the duplicate and element itself if it occurs more than 1 in consecution. This type of problem can occur in day-day programming and other
4 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 This is a brute-force method to perform this particu
8 min read
Python - Ways to print longest consecutive list without considering duplicates element
Given, a list of Numbers, the task is to print the longest consecutive (Strictly) list, without considering duplicate elements. If there is more than one answer, print anyone. These type of problem are quite common while working on some web development projects in Django or flask. Below are some ways to solve the above task. Note : If there are mul
4 min read
Python - List product excluding duplicates
This article focuses on one of the operations of getting the unique list from a list that contains a possible duplicated and finding its product. This operation has a large no. of applications and hence its knowledge is good to have. Method 1: Naive method In the naive method, we simply traverse the list and append the first occurrence of the eleme
6 min read
Python - Difference of List keeping duplicates
The problem of finding difference between list, i.e removing elements that occur in one list and not in other is discussed before. But the usage of sets ignores duplicates and we sometimes, require to remove the exact elements that occur in lists. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop This is brute wa
6 min read
Python List: Remove Duplicates And Keep The Order
Python lists are versatile data structures that allow the storage of multiple elements in a single variable. While lists provide a convenient way to manage collections of data, duplicates within a list can sometimes pose challenges. In this article, we will explore five different methods to remove duplicates from a Python list while preserving the
4 min read
Python - Ways to remove duplicates from list
This article focuses on one of the operations of getting a unique list from a list that contains a possible duplicate. Removing duplicates from list operation has a large number of applications and hence, its knowledge is good to have in Python. Ways to Remove duplicates from the list:Below are the methods that we will cover in this article: Using
10 min read
Python | Removing dictionary from list of dictionaries
The common utility to remove the dictionary corresponding to particular key in a list of dictionaries is also a problem whose concise version is always helpful. This has application in web development due to the introduction of No-SQL databases, which work mostly on Key-Value pairs. Let's discuss certain ways in which this task can be performed. Me
6 min read
Python Program For Removing Middle Points From a Linked List Of Line Segments
Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked List should be changed to following (0,10)->(7
4 min read
Python | Print list after removing element at given index
In this article, we will see how we can remove elements at a given index and print the list afterward. Example Input1: list = [10, 20, 30, 40, 50] index = 2 Output1: [10, 20, 40, 50] Input2: list = [10, 20, 40, 50] index = 0 Output2: [20, 40, 50] Print list after removing element at given indexBelow are the methods by which we can remove element at
3 min read
Python Program For Removing Every K-th Node Of The Linked List
Given a singly linked list, Your task is to remove every K-th node of the linked list. Assume that K is always less than or equal to length of Linked List.Examples : Input: 1->2->3->4->5->6->7->8 k = 3 Output: 1->2->4->5->7->8 As 3 is the k-th node after its deletion list would be 1->2->4->5->6->7->8 And now 4 is the starting node then from it, 6 w
3 min read
Python | Removing duplicate dicts in list
Removal of duplicates is essential in many applications. A list of dictionaries is quite common and sometimes we require to duplicate the duplicated. Let's discuss certain ways in which we can remove duplicate dictionaries in a list in Python. Using Loop for Removing duplicate dictionaries in a listThe basic method that comes to our mind while perf
4 min read
Practice Tags :