Skip to content

Instantly share code, notes, and snippets.

@89465127
Created March 30, 2013 05:45
Show Gist options
  • Save 89465127/5275551 to your computer and use it in GitHub Desktop.
Save 89465127/5275551 to your computer and use it in GitHub Desktop.
Examples of map() built-in that follow the official python documentation at http://docs.python.org/2/library/functions.html#map
# map(function, iterable, ...)
#
# http://docs.python.org/2/library/functions.html#map
nums = range(5)
extended_nums = range(10)
double = lambda x: x*2
concat = lambda x,y: "{x}{y}".format(x=x, y=y)
print '\nApply function to every item of iterable and return a list of the results.'
print map(double, nums)
print '\nIf additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.'
print map(concat, nums, nums)
print '\nIf one iterable is shorter than another it is assumed to be extended with None items.'
print map(concat, nums, extended_nums)
print '\nIf function is None, the identity function is assumed;'
print map(None, nums)
print '\nif there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation).'
print map(None, nums, extended_nums)
print '\nThe iterable arguments may be a sequence or any iterable object; the result is always a list.'
print map(None, range(3)) == map(None, xrange(3)) == map(None, (0,1,2)) == [0, 1, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment