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 Extract specific keys from dictionary?
Dictionaries are one of the most extensively used data structures in Python. They contain data in the form of key-value pairs. Sometimes you need to extract only specific keys from a dictionary to create a new dictionary. Python provides several approaches to accomplish this task efficiently.
Using Dictionary Comprehension with Set Intersection
This approach uses dictionary comprehension with set intersection to filter keys. The & operator finds common keys between the dictionary keys and your desired keys ?
Example
schedule = {'Sun': '2 PM', 'Tue': '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'}
# Given dictionary
print("Given dictionary:", schedule)
# Extract specific keys using set intersection
desired_keys = {'Fri', 'Sun'}
result = {key: schedule[key] for key in schedule.keys() & desired_keys}
# Result
print("Dictionary with given keys:", result)
Given dictionary: {'Sun': '2 PM', 'Tue': '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'}
Dictionary with given keys: {'Fri': '9 PM', 'Sun': '2 PM'}
Using dict() Constructor
This approach uses the dict() constructor with a generator expression. It includes a safety check using if k in dictA to avoid KeyError for missing keys ?
Example
schedule = {'Sun': '2 PM', 'Tue': '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'}
# Given dictionary
print("Given dictionary:", schedule)
# Extract specific keys using dict() constructor
desired_keys = ['Fri', 'Wed']
result = dict((k, schedule[k]) for k in desired_keys if k in schedule)
# Result
print("Dictionary with given keys:", result)
Given dictionary: {'Sun': '2 PM', 'Tue': '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'}
Dictionary with given keys: {'Fri': '9 PM', 'Wed': '3 PM'}
Using Simple Dictionary Comprehension
The most readable approach uses basic dictionary comprehension with the in operator to check if keys exist ?
Example
schedule = {'Sun': '2 PM', 'Tue': '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'}
# Given dictionary
print("Given dictionary:", schedule)
# Extract specific keys using simple comprehension
desired_keys = ['Tue', 'Wed', 'Mon'] # Note: 'Mon' doesn't exist
result = {k: schedule[k] for k in desired_keys if k in schedule}
# Result
print("Dictionary with given keys:", result)
Given dictionary: {'Sun': '2 PM', 'Tue': '5 PM', 'Wed': '3 PM', 'Fri': '9 PM'}
Dictionary with given keys: {'Tue': '5 PM', 'Wed': '3 PM'}
Comparison
| Method | Syntax | Best For |
|---|---|---|
| Set Intersection | {k: d[k] for k in d.keys() & keys} |
When you have many keys to extract |
| Dict Constructor | dict((k, d[k]) for k in keys if k in d) |
When working with tuples |
| Simple Comprehension | {k: d[k] for k in keys if k in d} |
Most readable and common approach |
Conclusion
Dictionary comprehension with the in operator is the most readable approach for extracting specific keys. Use set intersection for better performance when dealing with large dictionaries and many keys to extract.
