Last year, I passed my Mid-Level JavaScript certification and, in parallel, I worked on a Python project.
At one point, I had the need to filter a list and I realized I wasn’t as skilled with Python’s list handling as I did with JavaScript.
So here is the start of a series of articles about the methods of handling lists in Python, starting with the map() function.
Python’s map() function
The map() function in Python applies a given function, or lambda , to each item of an iterable (e.g., list) and returns a map object (iterator).
The syntax is the following: map(function, iterable)
Example of primitives
Let’s start simple with an array of primitives:
|
|
Example of objects
With objects, the logic isn’t much more complex:
|
|
About Performance
Note that in Python, you often need to convert the map object to a list (or another sequence type) to see the results immediately.
If you don’t need all the results at once, you can iterate over the map object directly, which can be more memory-efficient for large datasets.
Let’s look at an example.
Demonstration With an Example
When you use map() in Python, it returns an iterator object, not a list. This is a key difference from JavaScript’s map() which returns a new array immediately.
|
|
In this example, only one squared value exists in memory at any given time, rather than all one million values.
This following would end up being memory-taxing because we would have to load a million values into memory:
|
|
Instead, the following is a memory-efficient approach—processes one item at a time:
|
|
A Practical Use Cases
|
|
Important Considerations
You need to understand a few important aspects of map():
-
Map objects are single-use iterators. This is expected. Python behavior—
map()returns a lazy iterator that gets exhausted after one full traversal. The second loop silently produces no output.1 2 3 4 5 6 7 8 9numbers = [1, 2, 3, 4, 5] squared_map = map(lambda x: x**2, numbers) for num in squared_map: print(num) # Prints 1, 4, 9, 16, 25 # The iterator is now exhausted for num in squared_map: print(num) # Nothing will print -
You can combine
mapwith other iterators for efficient data processing:1 2 3 4 5 6 7 8from itertools import islice numbers = range(10000000) # Very large range squared_map = map(lambda x: x**2, numbers) # Get only the first 5 results without processing all items first_five = list(islice(squared_map, 5)) print(first_five) # Output: [0, 1, 4, 9, 16]This lazy evaluation approach is particularly valuable when working with datasets too large to fit in memory, as it allows you to process data in a streaming fashion.
Documentation References
Do you want to go further? Here are a few documentation articles to keep as a reference:
- https://www.w3schools.com/python/ref_func_map.asp
- https://www.digitalocean.com/community/tutorials/python-map-function
- https://docs.python.org/3/library/functions.html
- https://www.freecodecamp.org/news/python-map-explained-with-examples/
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Credit: Photo by Pixabay on Pexels.