Python | Priority key assignment in dictionary

Sometimes, while working with dictionaries, we have an application in which we need to assign a variable with a single value that would be from any of given keys, whichever occurs first in priority. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop
This task can be performed in a brute force manner using loop. In this, we can just loop through the dictionary keys and priority list. If we find the key, we break and assign variable with it’s value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Priority key assignment in dictionary
# Using loop
  
# Initialize dictionary
test_dict = {'gfg' : 6, 'is' : 4, 'for' : 2, 'CS' : 10}
  
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
  
# Initialize priority keys
prio_list = ['best', 'gfg', 'CS']
  
# Using loop
# Priority key assignment in dictionary
res = None
for key in test_dict:
    if key in prio_list :
        res = test_dict[key]
        break
  
# printing result 
print("The variable value after assignment : " + str(res))

chevron_right


Output :



The original dictionary : {'gfg': 6, 'is': 4, 'CS': 10, 'for': 2}
The variable value after assignment : 6

 

Method #2 : Using nested get()
This problem can be solved by nesting the get() to get the value on priority. Even if it provides a cleaner and one-liner alternative to solve the problem, it is not recommended if we have more candidate keys to check for priority.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Priority key assignment in dictionary
# Using nested get()
  
# Initialize dictionary
test_dict = {'gfg' : 6, 'is' : 4, 'for' : 2, 'CS' : 10}
  
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
  
# Initialize priority keys
prio_list = ['best', 'gfg', 'CS']
  
# Using nested get()
# Priority key assignment in dictionary
res = test_dict.get(prio_list[0], test_dict.get(prio_list[1],
                                  test_dict.get(prio_list[2])))
  
# printing result 
print("The variable value after assignment : " + str(res))

chevron_right


Output :

The original dictionary : {'gfg': 6, 'is': 4, 'CS': 10, 'for': 2}
The variable value after assignment : 6


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.