Open In App

Python | Get specific keys’ values

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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))


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




# 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))


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




# Python3 code to demonstrate working of
# Get specific keys' values
 
 
# 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
res = []
x = list(test_dict.keys())
y = list(test_dict.values())
for i in filt_keys:
    res.append(y[x.index(i)])
# printing result
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:

  1. Create a dictionary named test_dict and initialize it with key-value pairs.
  2. Create a list named filt_keys and initialize it with the keys that we want to extract values for.
  3. Print the original dictionary by converting it to a string and concatenating it with a string.
  4. 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.
  5. Within the for loop, use an if statement to check if the current key is present in the test_dict.
  6. If the key is present in the test_dict, append its value to the values list.
  7. After the for loop completes, print the filtered value list by converting it to a string and concatenating it with a string.

Python3




# 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
values = []
for key in filt_keys:
    if key in test_dict:
        values.append(test_dict[key])
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads