Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Last active October 11, 2015 12:17
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 andreagrandi/e200f8129840ea945258 to your computer and use it in GitHub Desktop.
Save andreagrandi/e200f8129840ea945258 to your computer and use it in GitHub Desktop.
# Define a lambda function that takes a number and double it
d = lambda x: x * 2
# Create a list containing: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l1 = [x for x in xrange(10)]
# Method #1 using map(...)
# output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print map(d, l1)
# Method #2 using list comprehension
# output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print [d(x) for x in l1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment