Python | Max/Min of tuple dictionary values
Last Updated :
22 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to find the min/max of tuple elements that are received as values of dictionary. We may have a problem to get index wise min/max. Let's discuss certain ways in which this particular problem can be solved.
Method #1 : Using tuple() + min()/max() + zip() + values() The combination of above methods can be used to perform this particular task. In this, we just zip together equi index values extracted by values() using zip(). Then find min/max value using respective function. Finally result is returned as index wise max/min values as a tuple.
Python3
# Python3 code to demonstrate working of
# Max / Min of tuple dictionary values
# Using tuple() + min()/max() + zip() + values()
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Max / Min of tuple dictionary values
# Using tuple() + min()/max() + zip() + values()
res = tuple(max(x) for x in zip(*test_dict.values()))
# printing result
print("The maximum values from each index is : " + str(res))
Output : The original dictionary is : {'is': (8, 3, 2), 'gfg': (5, 6, 1), 'best': (1, 4, 9)}
The maximum values from each index is : (8, 6, 9)
Method #2 : Using tuple() + map() + values() + * operator This is yet another way in which this task can be performed. The difference is that we use map() instead of loop and * operator for zipping the values together.
Python3
# Python3 code to demonstrate working of
# Max / Min of tuple dictionary values
# Using tuple() + map() + values() + * operator
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Max / Min of tuple dictionary values
# Using tuple() + map() + values() + * operator
res = tuple(map(min, *test_dict.values()))
# printing result
print("The minimum values from each index is : " + str(res))
Output : The original dictionary is : {'is': (8, 3, 2), 'gfg': (5, 6, 1), 'best': (1, 4, 9)}
The minimum values from each index is : (1, 3, 1)
Method #3 : Using max(),keys(),tuple() methods
Approach
- Initiate a for loop to traverse keys of dictionary
- Append the max of each tuple value to output list
- Convert the list to a tuple
- Display tuple
Python3
# Python3 code to demonstrate working of
# Max / Min of tuple dictionary values
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Max / Min of tuple dictionary values
x=list(test_dict.keys())
res=[]
for i in x:
res.append(max(test_dict[i]))
res=tuple(res)
# printing result
print("The maximum values from each index is : " + str(res))
OutputThe original dictionary is : {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
The maximum values from each index is : (6, 8, 9)
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4: Using list comprehension and tuple()
You can use a list comprehension to iterate over the values of each key in the dictionary and find the maximum value for each index. Then you can convert the resulting list into a tuple.
- Initialize a dictionary named test_dict with key-value pairs where the values are tuples of three integers. The keys are 'gfg', 'is', and 'best'.
- Print the original dictionary by using the print() function and converting the dictionary to a string using the str() function.
- Create a list comprehension that iterates through each key in the test_dict dictionary.
- For each key, call the max() function on the tuple value associated with that key to find the maximum value in the tuple.
- Add each maximum value to a new list called res.
- Convert the res list to a tuple using the tuple() function.
- Print the result by using the print() function and converting the tuple to a string using the str() function. The result is the maximum value from each index of the tuples in the test_dict dictionary.
Python3
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Max / Min of tuple dictionary values using Method #4
res = [max(test_dict[key]) for key in test_dict]
res = tuple(res)
# printing result
print("The maximum values from each index is : " + str(res))
OutputThe original dictionary is : {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
The maximum values from each index is : (6, 8, 9)
Time complexity: O(nm), where n is the number of keys in the dictionary and m is the length of the tuples.
Auxiliary space: O(n), where n is the number of keys in the dictionary.
Similar Reads
Python - Tuple value product in dictionary
Sometimes, while working with data, we can have a problem in which we need to find the product of tuple elements that are received as values of dictionary. We may have a problem to get index wise product. Letâs discuss certain ways in which this particular problem can be solved. Method #1 : Using tu
5 min read
Min and Max value in list of tuples-Python
The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
3 min read
Python - Summation of tuple dictionary values
Sometimes, while working with data, we can have a problem in which we need to find the summation of tuple elements that are received as values of dictionary. We may have a problem to get index wise summation. Letâs discuss certain ways in which this particular problem can be solved. Method #1: Using
4 min read
Python - Smallest K values in Dictionary
Many times while working with Python dictionary, we can have a particular problem to find the K minima of values in numerous keys. This problem is quite common while working with web development domain. Letâs discuss several ways in which this task can be performed. Smallest K values in Dictionary u
4 min read
Python - Dictionary Tuple Values Update
The task of updating tuple values in a dictionary involves modifying each tuple in the dictionary by applying a specific operation to its elements. In this case, the goal is to update each element of the tuple based on a given condition, such as multiplying each element by a constant. For example, g
3 min read
Python | Least Value test in Dictionary
While working with dictionary, we might come to a problem in which we require to ensure that all the values are atleast K in dictionary. This kind of problem can occur while checking status of start or checking for a bug/action that could have occurred. Letâs discuss certain ways in which this task
7 min read
Get Index of Values in Python Dictionary
Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe
3 min read
Dictionary items in value range in Python
In this article, we will explore different methods to extract dictionary items within a specific value range. The simplest approach involves using a loop.Using LoopThe idea is to iterate through dictionary using loop (for loop) and check each value against the given range and storing matching items
2 min read
Convert List of Tuples to Dictionary Value Lists - Python
The task is to convert a list of tuples into a dictionary where the first element of each tuple serves as the key and the second element becomes the value. If a key appears multiple times in the list, its values should be grouped together in a list.For example, given the list li = [(1, 'gfg'), (1, '
4 min read
Python - Dictionary Values Division
Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Letâs discuss division of like key values and ways to solve it in this article. Met
5 min read