map()
A built-in that applies a function to every item of one or more iterables and returns a lazy iterator.
In more detail
map(f, items) produces f(item) for each item only when asked, so list() is needed to see all the results at once. With several iterables, f receives one item from each and map stops at the shortest. Like every iterator, a map object is used up after one pass.
Example
m = map(lambda x: x * 10, [1, 2])
print(list(m), list(m)) # [10, 20] []Related terms
- filter() — A built-in that keeps the items of an iterable for which a function returns a true value.
- lambda — A small function written inline as an expression, such as lambda x: x * 2, with no name and a single expression as its body.
- iterator — An object that produces the items of a sequence one at a time, on request, instead of all at once.
Taught in
- The Class That Shared One List — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Sixty Five Minutes, Forty Items: How PCAP Is Run — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- List Comprehensions, Filtered And Nested — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep