Set from Dictionary Values - Python
Last Updated :
22 Jan, 2025
The task is to extract unique values from a dictionary and convert them into a set. In Python, sets are unordered collections that automatically eliminate duplicates. The goal is to extract all the values from the dictionary and store them in a set.
For example, given a dictionary like d = {'A': 4, 'B': 3, 'C': 7, 'D': 4}
, the goal is to retrieve all values from the dictionary and store them in a set, resulting in {3, 4, 7}
.
Using values()
This method is considered the most efficient because the values() method of the dictionary returns a view object that directly gives access to the dictionary’s values. Converting this view to a set using the set() allows us to easily get all unique values without the need for additional iterations.
Python
d = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
res = set(d.values())
print(res)
Explanation:
- d.values() returns a view of the dictionary’s values ([4, 3, 7, 3, 4]).
- set() converts these values into a set and removing duplicates and resulting in {4, 3, 7}.
Using Set Comprehension
Set comprehension is another efficient method that involves iterating over the dictionary’s values and adding them to a set in a single line. This method is almost as efficient as set(d.values()) but requires an explicit iteration over the values, making it slightly less optimal for very large dictionaries.
Python
d = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
res = {value for value in d.values()}
print(res)
Explanation: set comprehension iterate over the dictionary’s values and directly create a set and removes duplicates.
Using map()
map() applies a given function (e.g., lambda x: x) to each element in the dictionary’s values and then converts the result into a set. It’s more complex than other methods but useful when transformations are needed before adding values to the set.
Python
d = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
res = set(map(lambda x: x, d.values()))
print(res)
Explanation:
- d.values() retrieves the dictionary's values ([4, 3, 7, 3, 4]).
- map() applies an identity lambda (lambda x: x) to each value and resulting in the same values.
- set() removes duplicates, resulting in the set {4, 3, 7}.
Using for loop
Manually iterating over the dictionary’s values and adding them to a set is valid but less efficient than using built-in methods like set(d.values())
or set comprehension due to loop overhead.
Python
d = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
res = set()
for value in d.values():
res.add(value)
print(res)
Explanation:
for
loop iterates over the values of the dictionary d using d.values()
.- For each value in the dictionary d it is added to the set
res
using the add()
method.
Similar Reads
Python - Add Values to Dictionary of List A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 min read
Python - Remove Item from Dictionary There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict
3 min read
Python - Dictionary Tuple Values Update The task of updating tuple values in a dictionary involves modifying each tuple in the dictionary by applying a specific operation to its elements. In this case, the goal is to update each element of the tuple based on a given condition, such as multiplying each element by a constant. For example, g
3 min read
How to Add Values to Dictionary in Python The task of adding values to a dictionary in Python involves inserting new key-value pairs or modifying existing ones. A dictionary stores data in key-value pairs, where each key must be unique. Adding values allows us to expand or update the dictionary's contents, enabling dynamic manipulation of d
3 min read
Python | Increment value in dictionary In Python, dictionaries store data as keyâvalue pairs. If a key already exists, its value can be updated or incremented. This is commonly used for counting occurrences, like word frequency or item counts. Let's discuss certain ways in which this task can be performed. Using defaultdict()defaultdict
3 min read