Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Extract specific keys from dictionary

  • Difficulty Level : Basic
  • Last Updated : 25 Mar, 2022

We have a lot of variations and applications of dictionary container in Python and sometimes, we wish to perform a filter of keys in dictionary, i.e extracting just the keys which are present in particular container. Let’s discuss certain ways in which this can be performed. Method #1 : Using dictionary comprehension + items() This problem can be performed by reconstruction using the keys extracted through items function that wish to be filtered and dictionary function makes the desired dictionary. 

Python3




# Python3 code to demonstrate
# Extracting specific keys from dictionary
# Using dictionary comprehension + items()
 
# initializing dictionary
test_dict = {'nikhil' : 1, "akash" : 2, 'akshat' : 3, 'manjeet' : 4}
 
# printing original list
print("The original dictionary : " + str(test_dict))
 
# Using dictionary comprehension + items()
# Extracting specific keys from dictionary
res = {key: test_dict[key] for key in test_dict.keys()
                               & {'akshat', 'nikhil'}}
 
# print result
print("The filtered dictionary is : " + str(res))
Output : 
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}

  Method #2 : Using dict() The dict function can be used to perform this task by converting the logic performed using list comprehension into a dictionary. 

Python3




# Python3 code to demonstrate
# Extracting specific keys from dictionary
# Using dict()
 
# initializing dictionary
test_dict = {'nikhil' : 1, "akash" : 2, 'akshat' : 3, 'manjeet' : 4}
 
# printing original list
print("The original dictionary : " + str(test_dict))
 
# Using dict()
# Extracting specific keys from dictionary
res = dict((k, test_dict[k]) for k in ['nikhil', 'akshat']
                                        if k in test_dict)
 
# print result
print("The filtered dictionary is : " + str(res))

C++




#include <iostream>
using namespace std;
 
int main() {
 
    cout<<"GFG!";
    return 0;
}
Output : 
The original dictionary : {'manjeet': 4, 'akshat': 3, 'akash': 2, 'nikhil': 1}
The filtered dictionary is : {'akshat': 3, 'nikhil': 1}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!