4 ms·
map and filter still exist. reduce was demoted into the stdlib instead of being builtin in 3.x. list comprehensions generalize to other types (generator expres
by emidln 14y ago
map and filter still exist. reduce was demoted into the stdlib instead of being builtin in 3.x.
list comprehensions generalize to other types (generator expressions (aka "lazy sequences"), set comprehensions, dict compresions)
Compare this syntax (using dict comprehensions in 2.7):
{k:v for k, v in something.iteritems() if v == 3}
to this syntax using map/filter:
dict(filter(None,
filter(lambda x: x if x[1] == 3 else None,
something.iteritems()))
More frustrating to me is that there isn't an imap/ifilter builtin (I understand that it's in stdlib, I'm just lazy).
- masklinn 14y ago> More frustrating to me is that there isn't an imap/ifilter builtin FWIW, alongside the demotion of reduce to the stdlib Python 3 changed that, the builtin `map` and `filter` now return lazy "views" (as do e.g. `dict.items`, `dict.keys` and `dict.values`): # python 2 >>> map(sys.stdout.write, ['foo', 'bar', 'baz']) foobarbaz[None, None, None] # python 3 >>> map(sys.stdout.write, ['foo', 'bar', 'baz']) <map object at 0x10071cfd0>
- emidln 14y agoI really wasn't awake when I wrote this, because that second example is awful. It should be: {k:v for k, v in something.iteritems() if v == 3} vs dict(filter(lambda x: x[1] == 3, something.iteritems()))