Open In App

Python | Priority key assignment in dictionary

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

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. 

Python3




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


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. 

Python3




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


Output

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

Method #3 : Using next()
This method uses the next() function to return the next item from an iterator that satisfies a certain condition. In this case, the condition is checking if the key is in the priority list.

Python3




# Python3 code to demonstrate working of
# Priority key assignment in dictionary
# Using next()
 
# 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 next()
# Priority key assignment in dictionary
res = next((val for key, val in test_dict.items() if key in prio_list), None)
   
# printing result
print("The variable value after assignment : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

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

Time complexity of O(n) where n is the number of items in the dictionary, because it iterates through all the items of the dictionary. 
Auxiliary space of O(1) because they only use a single variable to store the result.

Method #4: Using list comprehension and get()

  1. Initialize the dictionary with key-value pairs.
  2. Create a list prio_list with the keys in the order of priority.
  3. Use a list comprehension to iterate over prio_list, getting the value associated with each key in the dictionary using the get() method, and return the first non-None value.
  4. Assign the result to a variable.
  5. Print the result.

Python3




# 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 list comprehension and get()
# Priority key assignment in dictionary
res = next(test_dict.get(key) for key in prio_list if key in test_dict)
 
# printing result
print("The variable value after assignment : " + str(res))


Output

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

Time complexity: O(n), where n is the number of keys in the priority list.
Auxiliary space: O(1), since we’re only using a fixed number of variables regardless of the size of the input.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads