Python: Find a tuple, the smallest second index value from a list of tuples
Python List: Exercise - 60 with Solution
Write a Python program to find a tuple, the smallest second index value from a list of tuples.
Visual Presentation:
Sample Solution:
Python Code:
# Define a list 'x' containing tuples, where each tuple has two elements
x = [(4, 1), (1, 2), (6, 0)]
# Use the 'min' function to find the minimum element in the list 'x' based on a custom key
# The 'key' argument specifies a lambda function that calculates a key for each element in 'x'
# The lambda function computes the key as a tuple, where the first element is the second element of the tuple (n[1]),
# and the second element is the negation of the first element (-n[0])
# This effectively sorts the tuples first by their second element in ascending order and then by their first element in descending order
# The 'min' function returns the tuple with the minimum key value
print(min(x, key=lambda n: (n[1], -n[0])))
Sample Output:
(6, 0)
Flowchart:

Python Code Editor:
Previous: Write a Python program to check if the n-th element exists in a given list.
Next: Write a Python program to create a list of empty dictionaries.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join