students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
]
def is_gryffindor(s):
return s["house"] == "Gryffindor"
gryffindors = filter(is_gryffindor, students)
for gryffindor in sorted(gryffindors, key=lambda s: s["name"]):
print(gryffindor["name"])
filter takes two arguments
- the first is a function that returns
True or False
- the second is the sequence which is to be filtered
- furthermore, a lambda function can be used to simplify it, removing the need to have a defined function
gryffindors = filter(lambda s: s["house"] == "Gryffindor", students)