Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Min and Max value in list of tuples-Python
Next article icon

Python | Max/Min of tuple dictionary values

Last Updated : 22 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  1. Initiate a for loop to traverse keys of dictionary
  2. Append the max of each tuple value to output list
  3. Convert the list to a tuple 
  4. 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))
 
 
Output
The 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.

  1. 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’.
  2. Print the original dictionary by using the print() function and converting the dictionary to a string using the str() function.
  3. Create a list comprehension that iterates through each key in the test_dict dictionary.
  4. For each key, call the max() function on the tuple value associated with that key to find the maximum value in the tuple.
  5. Add each maximum value to a new list called res.
  6. Convert the res list to a tuple using the tuple() function.
  7. 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))
 
 
Output
The 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. 



Next Article
Min and Max value in list of tuples-Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences