'Searching for the minimum value in a list of tuple? [duplicate]

I have this function where it takes in a list of tuples as it's parameter, I'm trying to return the oldest person but in order to do that I need to find the minimum value of the age im just having trouble with this since it's in a list of tuple. I tried to use the min() function but it didn't work out.

def oldest_person(people: list):
    new_list = []
    age = 1977
    for persons in people:
        if persons[1] == age:
            new_list.append(persons[0])    
      
    return new_list


p1 = ("Adam", 1977)
p2 = ("Ellen", 1985)
p3 = ("Mary", 1995)
p4 = ("Ernest", 1997)
people = [p1, p2, p3, p4]
print(oldest_person(people))


Solution 1:[1]

You can use min, with a key function that looks at the second element of the tuples. Then simply select the first element of the returned tuple:

p1 = ("Adam", 1977)
p2 = ("Ellen", 1985)
p3 = ("Mary", 1995)
p4 = ("Ernest", 1997)
people = [p1, p2, p3, p4]
oldest_person = min(people, key=lambda p:p[1])[0]

Output:

Adam

Solution 2:[2]

You can use min/max functions or sort the people and access the first person (oldest) or the last person (youngest).

sorted_people = sorted(people, key=lambda person: person[1])
oldest = sorted_people[0]
youngest = sorted_people[-1]

Solution 3:[3]

Use a custom comparator to sort the list people according to the birthyear. Something like this should work:

from functools import cmp_to_key
p1 = ("Adam", 1977)
p2 = ("Ellen", 1985)
p3 = ("Mary", 1995)
p4 = ("Ernest", 1997)
people = [p1, p2, p3, p4]

# here, you sort the list by comparing the ages.
sorted(people, key=cmp_to_key(lambda person1, person2: person1[1] - person2[1]))

print("Olderst person is", people[0][0], "who was born",  people[0][1])

Solution 4:[4]

you hard coded the oldest person age and checking for the same age in the list. So, it only returns the person with the age you coded.

My solution is not optimal but might work perhaps.

def oldest_person(people):
    age = [person[1] for person in people]
    age.sort()
    new_list = []
    for i in range(len(people)):
        if age[i] == people[i][1]:
            new_list.append(people[i][0])
    print(new_list)

p1 = ("Adam", 1977)
p2 = ("Ellen", 1985)
p3 = ("Mary", 1995)
p4 = ("Ernest", 1997)
people = [p1, p2, p3, p4]
oldest_person(people)

Output:

['Adam', 'Ellen', 'Mary', 'Ernest']

Solution 5:[5]

You can use Python's "min" function and "itemgetter" to find the minimum value in the second index of the tuples, then return the first element to get the name.

from operator import itemgetter

def oldest_person(people):
    return min(people, key=itemgetter(1))[0]

p1 = ("Adam", 1977)
p2 = ("Ellen", 1985)
p3 = ("Mary", 1995)
p4 = ("Ernest", 1997)
people = [p1, p2, p3, p4]

print(oldest_person(people)) # expected output: Adam

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick
Solution 2 Fabio Craig Wimmer Florey
Solution 3 Jhanzaib Humayun
Solution 4 Chaitu
Solution 5