We have a lot of variations and applications of dictionary containers in Python and sometimes, we wish to perform a filter of keys in a dictionary, i.e extracting just the keys which are present in the particular container. Let’s discuss certain ways in which this can be performed.
Extract specific keys from dictionary using dictionary comprehension + items()
This problem can be performed by reconstruction using the keys extracted through the items function that wishes to be filtered and the dictionary function makes the desired dictionary.
Python3
test_dict = { 'nikhil' : 1 , "akash" : 2 , 'akshat' : 3 , 'manjeet' : 4 }
print ( "The original dictionary : " + str (test_dict))
res = {key: test_dict[key] for key in test_dict.keys()
& { 'akshat' , 'nikhil' }}
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}
The time complexity of the given code is O(n), where n is the number of elements in the dictionary.
The space complexity of the given code is O(k), where k is the number of keys extracted from the dictionary.
Extract specific keys from the dictionary using dict()
The dict() function can be used to perform this task by converting the logic performed using list comprehension into a dictionary.
Python3
test_dict = { 'nikhil' : 1 , "akash" : 2 , 'akshat' : 3 , 'manjeet' : 4 }
print ( "The original dictionary : " + str (test_dict))
res = dict ((k, test_dict[k]) for k in [ 'nikhil' , 'akshat' ]
if k in test_dict)
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}
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.
Extract specific keys from dictionary using filter()
This method involves using the filter() function to filter the keys in the dictionary:
Python3
test_dict = { 'nikhil' : 1 , "akash" : 2 , 'akshat' : 3 , 'manjeet' : 4 }
print ( "The original dictionary : " + str (test_dict))
keys_to_extract = [ 'nikhil' , 'akshat' ]
res = dict ( filter ( lambda item: item[ 0 ] in keys_to_extract, test_dict.items()))
print ( "The filtered dictionary is : " + str (res))
|
Output
The original dictionary : {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The filtered dictionary is : {'nikhil': 1, 'akshat': 3}
Time complexity: O(n)
Auxiliary Space: O(n) for storing the result
Extract specific keys from dictionary Using a for loop and a conditional statement
Initialize the dictionary:
- Define the list of keys to be extracted.
- Initialize an empty dictionary to store the extracted keys and their values.
- Use a for loop and a conditional statement to check if each key in the dictionary is in the list of keys to be extracted. If it is, add the key-value pair to the extracted dictionary.
- Print the extracted dictionary.
Python3
test_dict = { 'nikhil' : 1 , "akash" : 2 , 'akshat' : 3 , 'manjeet' : 4 }
print ( "The original dictionary is:" , test_dict)
keys_to_extract = [ 'nikhil' , 'akshat' ]
extracted_dict = {}
for key, value in test_dict.items():
if key in keys_to_extract:
extracted_dict[key] = value
print ( "The extracted dictionary is:" , extracted_dict)
|
Output
The original dictionary is: {'nikhil': 1, 'akash': 2, 'akshat': 3, 'manjeet': 4}
The extracted dictionary is: {'nikhil': 1, 'akshat': 3}
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(k), where k is the number of keys to be extracted
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jul, 2023
Like Article
Save Article