Python map function

[Total: 1   Average: 5/5]

The map() function applies a given function to each item of an iterable (list, tuple etc.) and returns a list of the results.

The syntax of map() is:

map(function, iterable, ...)

map() Parameter

  • function – map() passes each item of the iterable to this function.
  • iterable iterable which is to be mapped

You can pass more than one iterable to the map() function.


Return Value from map()

The map() function applies a given to function to each item of an iterable and returns a list of the results.

The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) and so on.

Example: How map() works?

def myfunc(a):
return len(a)

x = map(myfunc, ('apple', 'banana', 'cherry'))

print(x)

#convert the map into a list, for readability:
print(list(x))

output

<map object at 0x056D44F0>
['5', '6', '6']

admin
Leave a Reply

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages