Skip to content

Instantly share code, notes, and snippets.

@uniphil
Created August 14, 2015 01:30
Show Gist options
  • Select an option

  • Save uniphil/912526a0dbcd088dabb7 to your computer and use it in GitHub Desktop.

Select an option

Save uniphil/912526a0dbcd088dabb7 to your computer and use it in GitHub Desktop.
python map
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