Open In App

Convert a List of Dictionaries into a Set of Dictionaries

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

Python’s versatility allows developers to manipulate data in various ways. When working with a list of dictionaries, there might be scenarios where you want to convert it into a set of dictionaries to eliminate duplicates or for other reasons. In this article, we’ll explore three different methods to achieve this goal with code examples.

Convert A List Of Dictionaries Into A Set Of Dictionaries

Below, are the methods of Convert A List Of Dictionaries Into A Set Of Dictionaries in Python.

Convert A List Of Dictionaries Into A Set Of Dictionaries Using For Loop

In this example, the below code uses a loop to convert a list of dictionaries into a set of tuples, each containing the key-value pairs of a dictionary. The resulting set represents unique dictionaries from the original list based on their items.

Python3




# Using a loop
list_of_dicts = [{'name': 'a', 'age': 23}, {'name': 'b', 'age': 21}, {'name': 'c', 'age': 20}]
set_of_dicts = set()
 
for d in list_of_dicts:
    set_of_dicts.add(tuple(d.items()))
 
print(set_of_dicts)


Output

{(('name', 'a'), ('age', 23)), (('name', 'c'), ('age', 20)), (('name', 'b'), ('age', 21))}


Convert A List Of Dictionaries Into A Set Of Dictionaries Using List Comprehension

In this example, below code employs list comprehension to efficiently convert a list of dictionaries into a set of tuples, where each tuple encapsulates the key-value pairs of a dictionary. The set represents unique dictionaries from the original list based on their items.

Python3




# Using list comprehension
list_of_dicts = [{'name': 'a', 'age': 23}, {'name': 'b', 'age': 21}, {'name': 'c', 'age': 20}]
set_of_dicts = {tuple(d.items()) for d in list_of_dicts}
 
print(set_of_dicts)


Output

{(('name', 'a'), ('age', 23)), (('name', 'c'), ('age', 20)), (('name', 'b'), ('age', 21))}


Convert A List Of Dictionaries Into A Set Of Dictionaries Using map() Function

In this example, below code utilizes the `map` function along with a lambda expression to transform a list of dictionaries into a set of tuples, where each tuple contains the key-value pairs of a dictionary. The resulting set represents unique dictionaries from the original list based on their items.

Python3




# Using map function
list_of_dicts = [{'name': 'a', 'age': 23}, {'name': 'b', 'age': 21}, {'name': 'c', 'age': 20}]
set_of_dicts = set(map(lambda x: tuple(x.items()), list_of_dicts))
 
print(set_of_dicts)


Output

{(('name', 'a'), ('age', 23)), (('name', 'c'), ('age', 20)), (('name', 'b'), ('age', 21))}


Convert A List Of Dictionaries Into A Set Of Dictionaries Using a Custom Function

In this example, below code defines a custom function `dict_to_frozenset` that converts a dictionary into a frozenset of its key-value pairs. The set comprehension is then used to create a set of frozensets, representing unique dictionaries from the original list based on their items.

Python3




#  Using custom function
def dict_to_frozenset(d):
    return frozenset(d.items())
 
list_of_dicts = [{'name': 'a', 'age': 23}, {'name': 'b', 'age': 21}, {'name': 'c', 'age': 20}]
set_of_dicts = {dict_to_frozenset(d) for d in list_of_dicts}
 
print(set_of_dicts)


Output

{frozenset({('age', 21), ('name', 'b')}), frozenset({('age', 23), ('name', 'a')}), frozenset({('age', 20), ('name', 'c')})}


Conclusion

In conclusion , Converting a list of dictionaries into a set of dictionaries in Python can be achieved through various methods. Whether you choose to use core Python functionalities, custom functions, or external libraries like Pandas, each method offers a unique approach to accomplishing the task. Consider the specific requirements of your project and choose the method that best suits your needs.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads