Created
December 3, 2020 04:57
-
-
Save emma-k-alexandra/54b020e54d33ff7baadeb5bf2a1382ab to your computer and use it in GitHub Desktop.
reduce or comprehensions > map & filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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