Skip to content

Instantly share code, notes, and snippets.

@emma-k-alexandra
Created December 3, 2020 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emma-k-alexandra/54b020e54d33ff7baadeb5bf2a1382ab to your computer and use it in GitHub Desktop.
Save emma-k-alexandra/54b020e54d33ff7baadeb5bf2a1382ab to your computer and use it in GitHub Desktop.
reduce or comprehensions > map & filter
l = [0, 1, 2, 3, 4, 5, 6]
# w/ map & filter
>>> list(map(lambda a: a * 2, filter(lambda a: a > 2, l)))
[6, 8, 10, 12]
# w/ generator
>>> list(a * 2 for a in l if a > 2)
[6, 8, 10, 12]
# w/ reduce
>>> reduce(lambda accumulator, a: accumulator + [a * 2] if a > 2 else accumulator, l, [])
[6, 8, 10, 12]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment