Python | Priority key assignment in dictionary
Last Updated :
23 Apr, 2023
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
test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
prio_list = [ 'best' , 'gfg' , 'CS' ]
res = None
for key in test_dict:
if key in prio_list :
res = test_dict[key]
break
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
test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
prio_list = [ 'best' , 'gfg' , 'CS' ]
res = test_dict.get(prio_list[ 0 ], test_dict.get(prio_list[ 1 ],
test_dict.get(prio_list[ 2 ])))
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
test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
prio_list = [ 'best' , 'gfg' , 'CS' ]
res = next ((val for key, val in test_dict.items() if key in prio_list), None )
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 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()
- Initialize the dictionary with key-value pairs.
- Create a list prio_list with the keys in the order of priority.
- 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.
- Assign the result to a variable.
- Print the result.
Python3
test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 }
print ( "The original dictionary : " + str (test_dict))
prio_list = [ 'best' , 'gfg' , 'CS' ]
res = next (test_dict.get(key) for key in prio_list if key in test_dict)
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
Please Login to comment...