0

I want to make narrowed list inside a class using a variable defined in the same class

class Example:
    exampleList = [("Alice", "F", 20), ("Bob", "M", 19), ("Rachel", "F", 25), ("John", "M", 50)]
    minFemaleAge = min(item[2] for item in exampleList if item[1] == "F")
    modifiedExampleList = [(item[0], item[2]) for item in exampleList if item[2] > minFemaleAge]

But on execution I got the error

File "example.py", line 4, in <listcomp>
    modifiedExampleList = [(item[0], item[2]) for item in exampleList if item[2] > minFemaleAge]
                                                                                   ^^^^^^^^^^^^
NameError: name 'minFemaleAge' is not defined

Even though minFemaleAge is certainly defined on the moment of usage. Can you explain me please, why this happens and is there a way to circumvent this problem?

0 Answers0