Open In App

Python Convert Dict of Lists to Dict of Sets

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

In Python, converting a dictionary of lists to a dictionary of sets is a valuable process for optimizing data structures. This transformation enhances efficiency by eliminating duplicates and enabling efficient membership testing. Utilizing built-in functions like set() and list comprehension simplifies the conversion process.

Example :

Input: dictionary_lists = {'key1': [1, 2, 3],
'key2': [4, 5, 6],
'key3': [7, 8, 9]}
Output: {'key1': {1, 2, 3}, 'key2': {4, 5, 6}, 'key3': {8, 9, 7}}

Python Convert Dict of Lists to Dict of Sets

Below, are the ways to Python Convert Dict Of Lists To Dict Of Sets.

Convert Dict Of Lists To Dict Of Sets Using Dictionary Comprehension

In the below code, the for loop iterates each key-value pair in the dictionary of lists and converts the values that are present in the form of a list to the set using the comprehension method.

Python3
# Original dictionary of lists
dictionary_lists = {'key1': [1, 2, 3],
                    'key2': [4, 5, 6],
                    'key3': [7, 8, 9]}

# Converting dictionary of lists to dictionary of sets
dictionary_sets = {key: set(value) for key, value in dictionary_lists.items()}

# Display the result
print(dictionary_sets)

Output
{'key1': {1, 2, 3}, 'key2': {4, 5, 6}, 'key3': {8, 9, 7}}



Convert Dict Of Lists To Dict Of Sets Using lambda Function

In below code, the lambda function takes key-value pair and returns a tuple where the key is stored and value is converted to set using set() function. The result of map() is passed to the dict() constructor to generate dictionary of sets.

Python3
dictionary_lists = {'key1': [1, 2, 3],
                    'key2': [4, 5, 6],
                    'key3': [7, 8, 9]}

# Using map and set
dictionary_sets = dict(
    map(lambda item: (item[0], set(item[1])), dictionary_lists.items()))

print(dictionary_sets)

Output
{'key1': {1, 2, 3}, 'key2': {4, 5, 6}, 'key3': {8, 9, 7}}



Convert Dict Of Lists To Dict Of Sets Using zip() Function

In below code, the dictionary_lists.keys() return the keys and dictionary_lists.values() return the values which are in form of lists will be converted in to sets using set keyword. zip() functions combines the keys and converted set values in the form of tuple.

Python3
dictionary_lists = {'key1': [1, 2, 3],
                    'key2': [4, 5, 6],
                    'key3': [7, 8, 9]}

# Using zip and dict constructor
dictionary_sets = dict(
    zip(dictionary_lists.keys(), map(set, dictionary_lists.values())))

print(dictionary_sets)

Output
{'key1': {1, 2, 3}, 'key2': {4, 5, 6}, 'key3': {8, 9, 7}}



Loop with Dictionary Comprehension

This approach utilizes a dictionary comprehension along with a loop to iterate over the keys of the original dictionary. It creates a new dictionary where each key is paired with a set obtained from the corresponding list value in the original dictionary.

Python
# Nikunj Sonigara
dictionary_lists = {
    'key1': [1, 2, 3],
    'key2': [4, 5, 6],
    'key3': [7, 8, 9]
}

# Convert dictionary of lists to dictionary of sets
dictionary_sets = {key: set(dictionary_lists[key]) for key in dictionary_lists}

# Display the result
print(dictionary_sets)

Output
{'key3': set([8, 9, 7]), 'key2': set([4, 5, 6]), 'key1': set([1, 2, 3])}

Conclusion

In conclusion, converting a dictionary of lists to a dictionary of sets in Python offers a streamlined approach to enhance data manipulation, eliminate duplicates, and enable efficient membership testing. This transformation provides a versatile and optimized data structure, particularly beneficial for handling datasets with unique values and unordered collections.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads