YouTip LogoYouTip

Python3 Func Map

# Python3.x Python3 map() Function [![Image 3: Python3 Built-in Functions](#) Python3 Built-in Functions](#) * * * ## Description The map() function applies a given function to each item of an iterable (like a list, tuple, etc.) and returns an iterator. The first parameter, function, is called with each element from the iterable sequences as its argument. It returns a new iterator containing the return values from each function call. ### Syntax Here is the syntax for the map() method: map(function, iterable, ...) ### Parameters * function -- A function * iterable -- One or more sequences ## Return Value Returns an iterator. * * * ## Examples The following examples demonstrate the usage of map(): ## Python3.x Examples >>>def square(x) : # Calculate square number ... return x ** 2 ... >>>map(square,[1,2,3,4,5])# Calculate the square of each element in the list # Returns an iterator >>>list(map(square,[1,2,3,4,5]))# Convert to list using list() [1,4,9,16,25] >>>list(map(lambda x: x ** 2,[1,2,3,4,5]))# Use lambda anonymous function [1,4,9,16,25] >>> [![Image 4: Python3 Built-in Functions](#) Python3 Built-in Functions](#)
← Java8 Nashorn JavascriptJava8 Streams β†’