6 ms·
Oh, yes, of course you're right, I didn't see that until now - did I say already, that I hate Python's list comprehensions or everything not totally simple?. S
by ReleaseCandidat 3y ago
Oh, yes, of course you're right, I didn't see that until now - did I say already, that I hate Python's list comprehensions or everything not totally simple?.
So that needs reduce:
def filter_func(acc, e):
if e["temp"] > 20:
acc[e["city"]] = e["temp"]
return acc
filtered_temps = functools.reduce(filter_func, temperatures, {})
- Izkata 3y agoTechnically that's a dict comprehension, not a list comprehension (though they are pretty much the same). The old style, before they existed, was to create a list of 2-tuples (key/value pairs) in a list comprehension and pass that to dict().