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 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 code to demonstrate working of # Selective key values in dictionary # Using list comprehension + get() # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Initialize key list key_list = ['gfg', 'best', 'CS'] # Using list comprehension + get() # Selective key values in dictionary res = [test_dict.get(key) for key in key_list] # printing result print("The values of Selective keys : " + str(res)) |
The original dictionary : {‘is’: 2, ‘gfg’: 1, ‘for’: 4, ‘CS’: 5, ‘best’: 3}
The values of Selective keys : [1, 3, 5]
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 code to demonstrate working of # Selective key values in dictionary # Using itemgetter() from operator import itemgetter # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Initialize key list key_list = ['gfg', 'best', 'CS'] # Using itemgetter() # Selective key values in dictionary res = list(itemgetter(*key_list)(test_dict)) # printing result print("The values of Selective keys : " + str(res)) |
The original dictionary : {‘is’: 2, ‘gfg’: 1, ‘for’: 4, ‘CS’: 5, ‘best’: 3}
The values of Selective keys : [1, 3, 5]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Filter dictionary key based on the values in selective list
- Python - Selective Key Values Summation
- Python | Multiplying Selective Values
- Python - Extract selective keys' values Including Nested Keys
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python - Extract target key from other key values
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python | Sum values for each key in nested dictionary
- Python - Convert key-values list to flat dictionary
- Python - Mapping key values to Dictionary
- Python - Unique Values of Key in Dictionary
- Python - Sort Dictionary key and values List
- Python - Assigning Key values to list elements from Value list Dictionary
- Python - Dictionary construction from front-rear key values
- Python - Extract values of Particular Key in Nested Values
- Python - Filter dictionary values in heterogenous dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python program to update a dictionary with the values from a dictionary list
- Python | Selective value selection in list of tuples
- Python | Selective Merge list every Nth position
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.

