0

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))
  • The `min()` and `max()` functions take an optional function argument, which you can use to tell the `min()` and `max()` functions what value to use when making comparisons: `min(people, key=lambda p: p[1])` – ddejohn Apr 23 '22 at 04:22

5 Answers5

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
Nick
  • 138,499
  • 22
  • 57
  • 95
0

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]
0

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])
Jhanzaib Humayun
  • 1,193
  • 1
  • 4
  • 10
0

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
Chowlett2
  • 128
  • 1
  • 1
  • 8
0

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']
Chaitu
  • 1