Python One Line Map

Rate this post

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.

Python’s 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.

Mastering the Python Map Function [+Video]

Consider the following map() one-liner that changes each element x of a list to the value of x+1:

print(list(map(lambda x: x + 1, [1, 2, 3])))
# [2, 3, 4]

You create a list with three elements. Then, you create an anonymous function that takes one argument (an integer in our case) and increments it by one. The 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 list(...) function.

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

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.

Get your Python One-Liners on Amazon!!