Python | Selective key values in dictionary
Last Updated :
27 Apr, 2023
Sometimes while working with Python dictionaries, we might have a problem in which we require to just get the selective key values from the dictionary. This problem can occur in web development domain. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using list comprehension + get() The combination of the above functions can be used to perform this particular task. In this, we access the values using the get method and traverse the dictionary using list comprehension.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 }
print ( "The original dictionary : " + str (test_dict))
key_list = [ 'gfg' , 'best' , 'CS' ]
res = [test_dict.get(key) for key in key_list]
print ( "The values of Selective keys : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The values of Selective keys : [1, 3, 5]
The time complexity of this implementation is O(n), where n is the number of keys in the key_list.
The auxiliary space complexity of this implementation is also O(n), since we are creating a new list res with one element for each key in the key_list.
Method #2: Using itemgetter() This single function can be used to perform this particular task. It is built in to perform this specific task. It takes chain of keys and returns the corresponding values as a tuple which can be type casted.
Python3
from operator import itemgetter
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 }
print ( "The original dictionary : " + str (test_dict))
key_list = [ 'gfg' , 'best' , 'CS' ]
res = list (itemgetter( * key_list)(test_dict))
print ( "The values of Selective keys : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The values of Selective keys : [1, 3, 5]
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using for loop
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 }
print ( "The original dictionary : " + str (test_dict))
key_list = [ 'gfg' , 'best' , 'CS' ]
res = []
for i in key_list:
res.append(test_dict[i])
print ( "The values of Selective keys : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The values of Selective keys : [1, 3, 5]
Time complexity: O(k), where k is the number of selected keys.
Auxiliary space: O(k), where k is the number of selected keys.
Method #4 : Using dictionary comprehension
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 }
print ( "The original dictionary : " + str (test_dict))
key_list = [ 'gfg' , 'best' , 'CS' ]
res = {key: test_dict[key] for key in key_list}.values()
print ( "The values of Selective keys : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The values of Selective keys : dict_values([1, 3, 5])
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #5: Using the map() function
The map() function takes two arguments – the function test_dict.get which retrieves the value for each key in the key_list, and the key_list itself. The map() function returns a map object, which is then converted to a list using the list() function.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 }
print ( "The original dictionary : " + str (test_dict))
key_list = [ 'gfg' , 'best' , 'CS' ]
res = list ( map (test_dict.get, key_list))
print ( "The values of Selective keys : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The values of Selective keys : [1, 3, 5]
Time complexity: O(n), where n is the length of the key_list.
Auxiliary space: O(n), where n is the length of the key_list.
Method 6: Using the operator module
This method uses the itemgetter function to get the values corresponding to the keys in key_list. The map function applies the itemgetter function to each key in key_list. The result is a list of values for the keys in key_list.
Python3
import operator
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 }
print ( "The original dictionary : " + str (test_dict))
key_list = [ 'gfg' , 'best' , 'CS' ]
res = list ( map (operator.itemgetter( * key_list), [test_dict]))
print ( "The values of Selective keys : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The values of Selective keys : [(1, 3, 5)]
Time complexity: O(k), where k is the length of the key_list.
Auxiliary space: O(k), because we are creating a new list res of length k to store the values corresponding to the keys in key_list.
Method#7: Using Recursive method.
Algorithm:
1. Base Case: If the input key list is empty, return an empty list.
2. Recursive Case: Take the first element of the key list and call it `head`. Take the rest of the key list and call it `tail`.
3. Recursively call `selective_values` on the `tail` of the key list.
4. If `head` is a key in the input dictionary, append its value to the result list. Otherwise, return the result list without appending anything.
Python3
def selective_values(test_dict, key_list):
if not key_list:
return []
head = key_list[ 0 ]
tail = key_list[ 1 :]
res = selective_values(test_dict, tail)
return [test_dict[head]] + res if head in test_dict else res
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'CS' : 5 }
key_list = [ 'gfg' , 'best' , 'CS' ]
res = selective_values(test_dict, key_list)
print ( "The values of Selective keys : " + str (res))
|
Output
The values of Selective keys : [1, 3, 5]
Time Complexity: O(n) – The recursive function is called n times, once for each key in the input key list. Dictionary lookup has an average time complexity of O(1), so the overall time complexity is dominated by the recursive calls, which gives us O(n).
Auxiliary Space: O(n) – The recursive function creates a new list for each recursive call, so the space complexity is proportional to the size of the input key list. In the worst case, where all keys are present in the dictionary, the size of the output list is the same as the size of the input key list, so the space complexity is O(n).
Share your thoughts in the comments
Please Login to comment...