I'm learning how to use the filter
function.
This is the code I've written:
people = [{'name': 'Mary', 'height': 160},
{'name': 'Isla', 'height': 80},
{'name': 'Sam'}]
people2 = filter(lambda x: "height" in x, people)
As you can see what I'm trying to do is to remove all the dictionaries that don't contain the 'height'
key.
The code works properly, in fact if I do:
print(list(people2))
I get:
[{'name': 'Mary', 'height': 160}, {'name': 'Isla', 'height': 80}]
The problem is that if I do it twice:
print(list(people2))
print(list(people2))
the second time, I get an empty list.
Can you explain me why?