Python - Assign keys with Maximum element index
Given Dictionary with value lists, the task is to write a Python program to assign each key with an index of the maximum value in the value list.
Examples:
Input : test_dict = {"gfg" : [5, 3, 6, 3], "is" : [1, 7, 5, 3], "best" : [9, 1, 3, 5]} Output : {'gfg': 2, 'is': 1, 'best': 0} Explanation : Max element in "gfg"'s value is 6 at 2nd index, hence assigned 2. Input : test_dict = {"gfg" : [9, 3, 6, 3], "is" : [1, 7, 5, 3], "best" : [9, 1, 3, 5]} Output : {'gfg': 0, 'is': 1, 'best': 0 Explanation : Max element in "gfg"'s value is 9 at 0th index, hence assigned 0.
Method #1 : Using max() + loop + index()
In this, we get the index of the maximum element from the value list using max() and index(). Loop is used for the task of iteration of keys in the dictionary.
# Python3 code to demonstrate working of
# Assign keys with Maximum element index
# Using max() + index() + loop
# initializing dictionary
test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = dict()
for key in test_dict:
# using index() to get required value
res[key] = test_dict[key].index(max(test_dict[key]))
# printing result
print("The maximum index assigned dictionary : " + str(res))
# Python3 code to demonstrate working of
# Assign keys with Maximum element index
# Using max() + index() + loop
# initializing dictionary
test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = dict()
for key in test_dict:
# using index() to get required value
res[key] = test_dict[key].index(max(test_dict[key]))
# printing result
print("The maximum index assigned dictionary : " + str(res))
Output:
The original dictionary is : {'gfg': [5, 3, 6, 3], 'is': [1, 7, 5, 3], 'best': [9, 1, 3, 5]}
The maximum index assigned dictionary : {'gfg': 2, 'is': 1, 'best': 0}
Method #2 : Using dictionary comprehension + max() + index()
In this, we perform task of getting result using dictionary comprehension shorthand variation of above method.
# Python3 code to demonstrate working of
# Assign keys with Maximum element index
# Using dictionary comprehension + max() + index()
# initializing dictionary
test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# using dictionary comprehension as one liner alternative
res = {key: test_dict[key].index(max(test_dict[key])) for key in test_dict}
# printing result
print("The maximum index assigned dictionary : " + str(res))
# Python3 code to demonstrate working of
# Assign keys with Maximum element index
# Using dictionary comprehension + max() + index()
# initializing dictionary
test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# using dictionary comprehension as one liner alternative
res = {key: test_dict[key].index(max(test_dict[key])) for key in test_dict}
# printing result
print("The maximum index assigned dictionary : " + str(res))
Output:
The original dictionary is : {'gfg': [5, 3, 6, 3], 'is': [1, 7, 5, 3], 'best': [9, 1, 3, 5]}
The maximum index assigned dictionary : {'gfg': 2, 'is': 1, 'best': 0}
Method #3: Using a loop and the enumerate() function
Step-by-step algorithm:
- Initialize a dictionary test_dict with keys as strings and values as lists of integers.
- Print the original dictionary test_dict.
- Initialize an empty dictionary res.
- For each key-value pair in the dictionary test_dict, do the following:
a. Get the maximum value of the list of integers using the max() function.
b. Get the index of the maximum value in the list of integers using a list comprehension.
c. Assign the key and the index value to the res dictionary. - Print the resulting dictionary res.
# initializing dictionary
test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = dict()
for key, values in test_dict.items():
max_val = max(values)
max_idx = [i for i, v in enumerate(values) if v == max_val][0]
res[key] = max_idx
# printing result
print("The maximum index assigned dictionary : " + str(res))
# initializing dictionary
test_dict = {"gfg": [5, 3, 6, 3], "is": [1, 7, 5, 3], "best": [9, 1, 3, 5]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
res = dict()
for key, values in test_dict.items():
max_val = max(values)
max_idx = [i for i, v in enumerate(values) if v == max_val][0]
res[key] = max_idx
# printing result
print("The maximum index assigned dictionary : " + str(res))
Output
The original dictionary is : {'gfg': [5, 3, 6, 3], 'is': [1, 7, 5, 3], 'best': [9, 1, 3, 5]} The maximum index assigned dictionary : {'gfg': 2, 'is': 1, 'best': 0}
Time Complexity: O(nm), where n is the number of keys in the dictionary test_dict and m is the length of the longest list of integers in the dictionary.
Auxiliary Space: O(n), where n is the number of keys in the dictionary test_dict.