Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Set from dictionary values
In Python, a dictionary is an implementation of a data structure known as an associative array. A dictionary is made up of key-value pairs, where each key maps to its corresponding value.
In this article, we will learn how to extract unique values from a dictionary and convert them into a set, which automatically removes duplicates.
Example Overview
Let's start with an example to understand what we want to achieve ?
# Input dictionary with duplicate values
input_dict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
print("Input dictionary:", input_dict)
print("Dictionary values:", list(input_dict.values()))
# Convert to set (removes duplicates)
result_set = set(input_dict.values())
print("Set from dictionary values:", result_set)
Input dictionary: {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
Dictionary values: [5, 10, 15, 5]
Set from dictionary values: {10, 5, 15}
Notice how the duplicate value 5 appears only once in the final set.
Method 1: Using values() and set() Functions
The most straightforward approach uses the values() method to extract dictionary values and set() to create a set ?
# Input dictionary
input_dict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
# Extract values and convert to set
result_set = set(input_dict.values())
print("Set from dictionary values:", result_set)
print("Number of unique values:", len(result_set))
Set from dictionary values: {10, 5, 15}
Number of unique values: 3
Method 2: Using Set Comprehension
Set comprehension provides a concise way to create a set from dictionary values ?
# Input dictionary
input_dict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
# Using set comprehension
result_set = {value for value in input_dict.values()}
print("Set from dictionary values:", result_set)
# Alternative: using keys to access values
result_set2 = {input_dict[key] for key in input_dict}
print("Alternative approach:", result_set2)
Set from dictionary values: {10, 5, 15}
Alternative approach: {10, 5, 15}
Method 3: Using Counter() Function
The Counter from collections module counts value frequencies and can help extract unique values ?
from collections import Counter
# Input dictionary
input_dict = {'hello': 5, 'tutorialspoint': 10, 'users': 15, 'python': 5}
# Count frequency of values
value_counts = Counter(input_dict.values())
print("Value frequencies:", value_counts)
# Extract unique values as a set
result_set = set(value_counts.keys())
print("Set from dictionary values:", result_set)
Value frequencies: Counter({5: 2, 10: 1, 15: 1})
Set from dictionary values: {10, 5, 15}
Comparison
| Method | Syntax | Performance | Best For |
|---|---|---|---|
set(dict.values()) |
Simple | Fastest | General use |
| Set comprehension | Readable | Fast | When filtering is needed |
Counter() |
Complex | Slowest | When you need frequency info |
Practical Example
Here's a real-world example showing how to extract unique scores from a student database ?
# Student scores dictionary
students = {
'Alice': 85,
'Bob': 92,
'Charlie': 85,
'Diana': 78,
'Eve': 92,
'Frank': 88
}
# Get unique scores
unique_scores = set(students.values())
print("All scores:", list(students.values()))
print("Unique scores:", sorted(unique_scores))
print("Number of different score levels:", len(unique_scores))
All scores: [85, 92, 85, 78, 92, 88] Unique scores: [78, 85, 88, 92] Number of different score levels: 4
Conclusion
The most efficient way to create a set from dictionary values is using set(dict.values()). Use set comprehension for readable code with filtering, and Counter() when you also need frequency information.
