Find youngest member of a list of people


I’m learning Python right now and I have a problem with finding the youngest one in my list.

class ember :
    def __init__(self,name,age,sex,weight,): = str(name)
        self.age = int(age) = str(sex)
        self.weight = int(weight)

e1=ember("Pisti1", 13, "Ferfi", 45)
e2=ember("Pisti2", 14, "Ferfi", 46)
e3=ember("Pisti3", 15, "Ferfi", 120)
e4=ember("Pisti4", 16, "Ferfi", 48)
e5=ember("JÚLYA", 17, "Nő", 89)


for x in Lista:
    if x.age>maxx:
print (, x.age)

It only gives me the oldest (last) one in the list (JÚLYA).

I watched a couple of videos and tutorials about lists and list comprehensions but I don’t get it; can someone give me a "hint" or help me?

Asked By: Xaties Loco



You need to finding the minimum by a certain attribute. The standard way to do this in Python is (after putting import operator at the start of your code)

youngest = min(Lista, key=operator.attrgetter("age"))
print(, youngest.age)

Note that the middle section of your code (e1=.append(e5)) can be more concisely expressed as follows.

Lista = [
    ember("Pisti1", 13, "Ferfi", 45),
    ember("Pisti2", 14, "Ferfi", 46),
    ember("Pisti3", 15, "Ferfi", 120),
    ember("Pisti4", 16, "Ferfi", 48),
    ember("JÚLYA", 17, "Nő", 89),
Answered By: LeopardShark

Just remove everything down from maxx=1000000000000 and add the following code:

youngest = Lista[0]

for x in Lista:
    if x.age<youngest.age:

print (, youngest.age)
Answered By: Legionarks
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.