Given a Dictionary with keys and values that are lists, the following program displays key of the value whose range in maximum.
Range = Maximum number-Minimum number
Input : test_dict = {“Gfg” : [6, 2, 4, 1], “is” : [4, 7, 3, 3, 8], “Best” : [1, 0, 9, 3]}
Output : Best
Explanation : 9 – 0 = 9, Maximum range compared to all other list given as values
Input : test_dict = {“Gfg” : [16, 2, 4, 1], “Best” : [1, 0, 9, 3]}
Output : Gfg
Explanation : 16 – 1 = 15, Maximum range compared to all other list given as values
Method 1: Using max(), min() and loop
In this, we get max() and min() of each list and perform difference to find range. This value is then stored and max difference of all such values is computed by applying max() at the result list.
Python3
test_dict = { "Gfg" : [ 6 , 2 , 4 , 1 ], "is" : [ 4 , 7 , 3 , 3 , 8 ], "Best" : [ 1 , 0 , 9 , 3 ]}
print ( "The original dictionary is : " + str (test_dict))
max_res = 0
for sub, vals in test_dict.items():
max_res = max (max_res, max (vals) - min (vals))
if max_res = = max (vals) - min (vals):
res = sub
print ( "The maximum element key : " + str (res))
|
OutputThe original dictionary is : {'Gfg': [6, 2, 4, 1], 'is': [4, 7, 3, 3, 8], 'Best': [1, 0, 9, 3]}
The maximum element key : Best
Time Complexity: O(n) where n is the number of elements in the dictionary “test_dict”. The sorted and itemgetter function is used to perform the task and it takes O(n*logn) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary “test_dict”.
Method 2: Using list comprehension, max() and min()
In this, we compute the maximum range, and then extract the key which matches that difference using list comprehension.
Python3
test_dict = { "Gfg" : [ 6 , 2 , 4 , 1 ], "is" : [ 4 , 7 , 3 , 3 , 8 ], "Best" : [ 1 , 0 , 9 , 3 ]}
print ( "The original dictionary is : " + str (test_dict))
max_res = max ([ max (vals) - min (vals) for sub, vals in test_dict.items()])
res = [sub for sub in test_dict if max (test_dict[sub]) - min (test_dict[sub]) = = max_res][ 0 ]
print ( "The maximum element key : " + str (res))
|
OutputThe original dictionary is : {'Gfg': [6, 2, 4, 1], 'is': [4, 7, 3, 3, 8], 'Best': [1, 0, 9, 3]}
The maximum element key : Best
Method 3: Finding the Key with the Maximum Difference in Values in a Dictionary.
- Initialize two variables max_diff and max_key to None.
- Iterate over the items in the dictionary using a for loop.
- For each item, calculate the difference between its maximum and minimum value using max() and min() functions.
- If max_diff is None or the difference for the current item is greater than max_diff, update max_diff and max_key to the current difference and key respectively.
- After the loop, max_key will contain the key with the maximum difference.
Python3
test_dict = { "Gfg" : [ 6 , 2 , 4 , 1 ], "is" : [ 4 , 7 , 3 , 3 , 8 ], "Best" : [ 1 , 0 , 9 , 3 ]}
print ( "The original dictionary is : " + str (test_dict))
max_diff = None
max_key = None
for key, value in test_dict.items():
diff = max (value) - min (value)
if max_diff is None or diff > max_diff:
max_diff = diff
max_key = key
print ( "The maximum element key : " + str (max_key))
|
OutputThe original dictionary is : {'Gfg': [6, 2, 4, 1], 'is': [4, 7, 3, 3, 8], 'Best': [1, 0, 9, 3]}
The maximum element key : Best
Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(1), as it only uses a constant amount of space to store the max_diff and max_key variables.
Method 4: Using the built-in ‘map’ function
- Define a function that computes the difference between the maximum and minimum value of a list.
- Use the ‘map’ function to apply the function to each value of the dictionary.
- Find the index of the maximum difference using the ‘index’ method of the resulting list.
- Use the ‘list’ method to retrieve the keys of the dictionary and get the corresponding key.
Python3
test_dict = { "Gfg" : [ 6 , 2 , 4 , 1 ], "is" : [ 4 , 7 , 3 , 3 , 8 ], "Best" : [ 1 , 0 , 9 , 3 ]}
def max_min_diff(lst):
return max (lst) - min (lst)
diffs = list ( map (max_min_diff, test_dict.values()))
max_idx = diffs.index( max (diffs))
max_key = list (test_dict.keys())[max_idx]
print ( "The maximum element key : " + str (max_key))
|
OutputThe maximum element key : Best
Time complexity: O(n*m), where n is the number of keys and m is the length of the longest list in the dictionary.
Auxiliary space: O(n), for creating the list of differences.