Created
August 14, 2015 01:30
-
-
Save uniphil/912526a0dbcd088dabb7 to your computer and use it in GitHub Desktop.
python map
This file contains hidden or 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
| my_list = [0, 1, 2, 3, 4, 5, 6] | |
| # if it's a very simple transformation, you can inline the function with `lambda` | |
| squared = map(lambda el: el**2, my_list) | |
| # squared should now be the list with all elements squared | |
| assert squared == [0, 1, 4, 9, 16, 25, 36] # passes, woo! | |
| # if it's more complicated, you can break out a function for it | |
| def square(el): | |
| # do whatever complicated stuff you need to do | |
| return el**2 | |
| squared = map(square, my_list) | |
| # squared should now be the list with all elements squared | |
| assert squared == [0, 1, 4, 9, 16, 25, 36] # passes, woo! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment