w3resource

Python: Find the maximum and minimum values in a given list of tuples

Python List: Exercise - 158 with Solution

Max and Min in List of Tuples

Write a Python program to find the maximum and minimum values in a given list of tuples.

Pictorial Presentation:

Python List: Find the maximum and minimum values in a given  list of tuples.

Sample Solution:

Python Code:

# Import the 'itemgetter' function from the 'operator' module.
from operator import itemgetter

# Define a function called max_min_list_tuples that takes a list of tuples 'class_students' as input.
def max_min_list_tuples(class_students):
    # Find the tuple with the maximum second element (index 1) in 'class_students' using 'itemgetter' and store it in 'return_max'.
    return_max = max(class_students, key=itemgetter(1))[1] 
    
    # Find the tuple with the minimum second element (index 1) in 'class_students' using 'itemgetter' and store it in 'return_min'.
    return_min = min(class_students, key=itemgetter(1))[1] 
    
    # Return the maximum and minimum values found in the list of tuples.
    return return_max, return_min

# Create a list of tuples 'class_students' where each tuple contains a class name and a student count.
class_students = [('V', 60), ('VI', 70), ('VII', 75), ('VIII', 72), ('IX', 78), ('X', 70)]

# Print a message indicating the original list with tuples.
print("Original list with tuples:")
print(class_students)

# Print a message indicating that the maximum and minimum values are being determined, and call the 'max_min_list_tuples' function.
print("\nMaximum and minimum values of the said list of tuples:")
print(max_min_list_tuples(class_students)) 

Sample Output:

Original list with tuples:
[('V', 60), ('VI', 70), ('VII', 75), ('VIII', 72), ('IX', 78), ('X', 70)]

Maximum and minimum values of the said list of tuples:
(78, 60)

Flowchart:

Flowchart: Find the maximum and minimum values in a given  list of tuples.

Python Code Editor:

Previous: Write a Python program to interleave multiple given lists of different lengths.
Next: Write a Python program to append the same value /a list multiple times to a list/list-of-lists.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-158.php