w3resource

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

Python Lambda: Exercise-51 with Solution

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

Sample Solution:

Python Code :

def max_min_list_tuples(class_students):
    return_max = max(class_students,key=lambda item:item[1])[1]
    return_min = min(class_students,key=lambda item:item[1])[1]
    return return_max, return_min
    
class_students = [('V', 62), ('VI', 68), ('VII', 72), ('VIII', 70), ('IX', 74), ('X', 65)]
print("Original list with tuples:")
print(class_students)
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', 62), ('VI', 68), ('VII', 72), ('VIII', 70), ('IX', 74), ('X', 65)]

Maximum and minimum values of the said list of tuples:
(74, 62)

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to remove specific words from a given list using lambda.

Next: Write a Python program to remove None value from a given list using lambda function.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Memoization using LRU cache:

import functools

@functools.lru_cache(maxsize=128)
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)