The single line of Python code is more powerful than you may expect. In fact, you can compress whole algorithms in a single line of Python code. In this tutorial, you’ll learn how to use the built-in
map() function in Python to one-linerize a critical step that would otherwise take multiple lines of Python code: to modify each element in a given iterable.
map() function applies a specific function to each element in a given iterable. It takes two arguments:
- Function: The function to apply on each element of an iterable. In most cases, it’s a lambda function to defined once and on the fly.
- Iterable: Each iterable element is modified according to the function defined in the first argument.
The result is a
map() object, an iterator that saves all mapped elements so that you can iterate over them.
print(list(map(lambda x: x + 1, [1, 2, 3]))) # [2, 3, 4]
You create a
map function applies the function to each element in the list and returns a new map object. This is converted back to a list using the
Try it yourself in our interactive code shell:
Exercise: Change the one-liner to calculate the square number of each list element.
Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.
The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.
Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.
You’ll also learn how to:
- Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
- Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
- Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
- Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
- Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.
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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.