map() Flashcards

1
Q

nums = [1, 2, 3, 4, 5]
def double(x):
return x * 2

What does map() do in Python, and how can you use it with the above function to double every number in List “x”?

A

map() applies a function to each item in a list (or other iterable).

doubled = list(map(double, nums))
print(doubled)

Output: [2, 4, 6, 8]

Syntax: map(function, iterable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly