Python | Get specific keys’ values

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Get specific keys' values
# Using list comprehension
  
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
  
# initializing keys
filt_keys = ['gfg', 'best']
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Extract specific value list from dictionary
# Using list comprehension
res = [test_dict[key] for key in filt_keys]
  
# printing result
print("Filtered value list is : " +  str(res))

chevron_right


Output :



The original dictionary is : {'is': 2, 'best': 3, 'gfg': 1}
Filtered value list is : [1, 3]

 

Method #2 : Using map() + get()
The combination of 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Get specific keys' values
# Using map() + get()
  
# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
  
# initializing keys
filt_keys = ['gfg', 'best']
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Extract specific value list from dictionary
# Using map() + get()
res = list(map(test_dict.get, filt_keys))
  
# printing result
print("Filtered value list is : " +  str(res))

chevron_right


Output :

The original dictionary is : {'is': 2, 'best': 3, 'gfg': 1}
Filtered value list is : [1, 3]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.