Python | Get specific keys’ values
Last Updated :
27 Apr, 2023
Sometimes, we require all the values, but many times, we have specified keys of whose value list we require. This is quite common problem for web development. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using list comprehension
This task can be performed using list comprehension adopted as the shorter way to perform the longer task of checking using loop. This offers a one liner approach to solve this problem.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
filt_keys = [ 'gfg' , 'best' ]
print ( "The original dictionary is : " + str (test_dict))
res = [test_dict[key] for key in filt_keys]
print ( "Filtered value list is : " + str (res))
|
Output :
The original dictionary is : {'is': 2, 'best': 3, 'gfg': 1}
Filtered value list is : [1, 3]
Time Complexity: O(n*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 #2: Using map() + get()
The combination of the above functions can offer a more compact solution for this task. The map function can be used to extend the logic to whole dictionary and get function is used to fetch key’s value.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
filt_keys = [ 'gfg' , 'best' ]
print ( "The original dictionary is : " + str (test_dict))
res = list ( map (test_dict.get, filt_keys))
print ( "Filtered value list is : " + str (res))
|
Output :
The original dictionary is : {'is': 2, 'best': 3, 'gfg': 1}
Filtered value list is : [1, 3]
Method #3 : Using keys(),values() and index() methods
- Store keys of dictionary in x(using keys())
- Store values of dictionary in y(using values())
- Initiate a for loop to traverse filter keys to get the values of those keys and store in list
- Display list
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
filt_keys = [ 'gfg' , 'best' ]
print ( "The original dictionary is : " + str (test_dict))
res = []
x = list (test_dict.keys())
y = list (test_dict.values())
for i in filt_keys:
res.append(y[x.index(i)])
print ( "Filtered value list is : " + str (res))
|
Output
The original dictionary is : {'gfg': 1, 'is': 2, 'best': 3}
Filtered value list is : [1, 3]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 4: using a simple for loop and a conditional statement
Approach:
- Create a dictionary named test_dict and initialize it with key-value pairs.
- Create a list named filt_keys and initialize it with the keys that we want to extract values for.
- Print the original dictionary by converting it to a string and concatenating it with a string.
- Create an empty list named values to store the values that are extracted from the dictionary.
Start a for loop that will iterate over each key in the filt_keys list.
- Within the for loop, use an if statement to check if the current key is present in the test_dict.
- If the key is present in the test_dict, append its value to the values list.
- After the for loop completes, print the filtered value list by converting it to a string and concatenating it with a string.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 }
filt_keys = [ 'gfg' , 'best' ]
print ( "The original dictionary is: " + str (test_dict))
values = []
for key in filt_keys:
if key in test_dict:
values.append(test_dict[key])
print ( "Filtered value list is: " + str (values))
|
Output
The original dictionary is: {'gfg': 1, 'is': 2, 'best': 3}
Filtered value list is: [1, 3]
Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(m), where m is the number of keys to be extracted from the dictionary.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...