That's kind of what we do when we write lambda row: dothings(row)
. It says "here's an unnamed function, that takes a parameter. Call that parameter 'row
' and pass it to dothings
".
How/why is it a dict ? It's actually not a dict, but that's not relevant here. Let's consider it's a dict.
filter
will iterate through the data we give it. Here's that data is your pyDataSet
. Iterating through a data structure means taking its elements one by one. So, it takes the elements of your pyDataSet
, which we'll consider as if they were dicts.
Then, filter
passes those elements to the filtering key. The key
parameter is the first one. In this case, we pass a lambda as argument. A lambda is an unnamed function, which means it can take parameters. The parameter it receives for each iteration, is the current element from data
.
You can think of it this way:
results = filter(filter_func, data)
is equivalent to
results = [element for element in data if filter_func(element)]
which is another way of doing this:
results = []
for element in data:
if filter_func(element):
results.append(element)
Here, element
is what we called row
before. It doesn't appear in the filter
version because the filter_func
argument is not a lambda. Note that nowhere in those 3 examples was filter_func
defined, so obviously you'd need to define it for the examples to work. You could create that function, or... do this instead:
results = [element for element in data if element > 5]
results = []
for element in data:
if element > 5:
results.append(element)
BUT filter
expects the key
to be callable. Which is why we use a lambda, to make element > 5
callable:
results = filter(lambda element: element > 5, data)
element
will be whatever items in data
are. Let's say data
is a string instead, then the key
will receive the characters from that string, one after the other. For example:
vowels = filter(lambda c: c in "aeiouy", "FooBarBazPoxWuz")
# vowels contains o, o, a, a, o, u
If you make data a list of integers, the key
will receive those integers, one at a time:
odds = filter(lambda n: n % 2 != 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])
# odds contains 1, 3, 5, 7, 9
Note that what we call the lambda's parameter doesn't matter. It's not because we call it row
that it will be a row from data. You can call it "shrinked_head" or "sunshine", it doesn't matter.
I hope I made things clearer and not the opposite :X
TL;DR:
Yea, pretty much.