Problem: Given multiple Python statements. How to write them as a Python One-Liner?
Example: Consider the following example of four statements in a block with uniform indentation:
a = 1 b = 2 c = a + b print(c)
How to write all four statements in a single line of code?
Solution: The answer is simple if all statements have a uniform indentation and there’s no nested block. In this case, you can use the semicolon as a separator between the statements:
a = 1; b = 2; c = a + b; print(c)
Let’s do some practice testing to learn and improve your Python skills:
Exercise: one-linerize the given code! Run the code and check if the one-liner does the same as the original code!
While this works beautifully, if all statements are not indented—does it still work if you have an indentation block that starts with the colon
: symbol after
Here’s an example of such a block:
for i in range(10): c = i ** 2 print (c)
You try the following one-liner using the semicolon as a separator between the two statements in the block
for i in range(10): c = i ** 2; print(c) ''' 0 1 4 9 16 25 36 49 64 81 '''
This works beautifully and Python understands what you are trying to do. However, if you have nested indentation blocks, this doesn’t work anymore.
Consider the following example:
for i in range(3): for j in range(3): print(i, j)
If you write this in a single line, Python throws a syntax error:
While you can discuss if this makes sense or not—given that the syntax is not ambiguous here—it doesn’t change the fact: nested block cannot be one-linerized in a straightforward way. But this doesn’t prevent us from doing it, right?
Nested Indentation Blocks
Summary: To make a Python one-liner out of any multi-line Python script, replace the new lines with a new line character
'n' and pass the result into the
exec(...) function. You can run this script from the outside (command line, shell, terminal) by using the command
python -c "exec(...)".
This method is very powerful and it allows you to compress any complicated multi-line script in a single line of Python code!
Python One-Liners Book
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 tips and tricks, regular expressions, machine learning, core data science topics, and 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.