The map() function in Python applies a specified function to each item in one or more iterables (like lists, tuples, or strings) and returns a map object, which is an iterator that yields transformed values on demand. This enables efficient, functional-style data processing without explicit loops.
Key Features:
Syntax:
map(function, iterable, ...)function: A callable (e.g., built-in, lambda, or user-defined function).iterable: One or more sequences to process.
Lazy Evaluation: In Python 3,
map()returns an iterator (not a list), so values are computed only when accessed—saving memory for large datasets.Multiple Iterables: If multiple iterables are provided, the function must accept as many arguments as there are iterables. Processing stops when the shortest iterable is exhausted.
Examples:
Using a lambda:
numbers = [1, 2, 3, 4] squared = list(map(lambda x: x**2, numbers)) # Output: [1, 4, 9, 16]Using built-in functions:
str_nums = ["1", "2", "3"] int_nums = list(map(int, str_nums)) # Output: [1, 2, 3]Converting Celsius to Fahrenheit:
celsius = [0, 20, 37] fahrenheit = list(map(lambda c: (c * 9/5) + 32, celsius)) # Output: [32.0, 68.0, 98.6]With multiple iterables:
a = [1, 2, 3] b = [4, 5, 6] result = list(map(pow, a, b)) # Output: [1, 32, 729] (1^4, 2^5, 3^6)
Note: Always convert the map object to a list, tuple, or iterate directly using
list(map(...))to access results. The map object is exhausted after one use.
Videos
map isn't particularly pythonic. I would recommend using list comprehensions instead:
map(f, iterable)
is basically equivalent to:
[f(x) for x in iterable]
map on its own can't do a Cartesian product, because the length of its output list is always the same as its input list. You can trivially do a Cartesian product with a list comprehension though:
[(a, b) for a in iterable_a for b in iterable_b]
The syntax is a little confusing -- that's basically equivalent to:
result = []
for a in iterable_a:
for b in iterable_b:
result.append((a, b))
map doesn't relate to a Cartesian product at all, although I imagine someone well versed in functional programming could come up with some impossible to understand way of generating a one using map.
map in Python 3 is equivalent to this:
def map(func, iterable):
for i in iterable:
yield func(i)
and the only difference in Python 2 is that it will build up a full list of results to return all at once instead of yielding.
Although Python convention usually prefers list comprehensions (or generator expressions) to achieve the same result as a call to map, particularly if you're using a lambda expression as the first argument:
[func(i) for i in iterable]
As an example of what you asked for in the comments on the question - "turn a string into an array", by 'array' you probably want either a tuple or a list (both of them behave a little like arrays from other languages) -
>>> a = "hello, world"
>>> list(a)
['h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
>>> tuple(a)
('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd')
A use of map here would be if you start with a list of strings instead of a single string - map can listify all of them individually:
>>> a = ["foo", "bar", "baz"]
>>> list(map(list, a))
[['f', 'o', 'o'], ['b', 'a', 'r'], ['b', 'a', 'z']]
Note that map(list, a) is equivalent in Python 2, but in Python 3 you need the list call if you want to do anything other than feed it into a for loop (or a processing function such as sum that only needs an iterable, and not a sequence). But also note again that a list comprehension is usually preferred:
>>> [list(b) for b in a]
[['f', 'o', 'o'], ['b', 'a', 'r'], ['b', 'a', 'z']]