You may have read that Python’s godfather, Guido van Rossum, doesn’t like the
map() function too much. He argues that it can be easily replaced with list comprehension which is not only faster but also more readable and shorter. Here’s an example:
# Python Puzzle xs = list(range(10)) list_1 = list(map(lambda x: x*x, xs)) list_2 = [x*x for x in xs] print(list_1 == list_2)
You can solve the puzzle on the interactive Finxter app before reading on to test your Python skills:
As you may have guessed, both lists
list_2 contain the same data: a series of squared values.
The puzzle shows two ways to achieve the same result. Either you can use
map(function, list) and convert the resulting map object to a list or you iterate over each item with a list comprehension. Both ways lead to the same result, thus the output is
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.