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.
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://198.211.115.131/python-exercises/list/python-data-type-list-exercise-60.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics