Powerful Python One-Liners
This page is devoted to short programs that can perform powerful operations called Python One-Liners.
You may ask: why should I care? The answer is profound: if you cannot read and write one-liner code snippets, how can you ever hope to read and write more complicated codebases? Python one-liners can be just as powerful as a long and tedious program written in another language designed to do the same thing. In other languages (think: Java) this would be nearly impossible, but in Python, it's a lot easier to do. The trick is to think of something that will "do a lot with a little." Most importantly, reading and writing about Python one-liners (e.g., in this post) is a lot of fun! There's even a whole subculture around who can write the shortest code for a given problem.
It would be awesome if this page expanded to the point where it needs some sort of organization system. (Edit: The one-liners are now sorted more or less by ease-of-understanding -- from simple to hard. Please use a "sorted insert" for your new one-liner.)
The source code is contributed from different Python coders --- Thanks to all of them! Special thanks to the early contributor JAM.
Of course, there is debate on whether one-liners are even Pythonic. As a rule of thumb: if you use one-liners that are confusing, difficult to understand, or to show off your skills, they tend to be Unpythonic. However, if you use well-established one-liner tricks such as list comprehension or the ternary operator, they tend to be Pythonic.
So, use your one-liner superpower wisely!
Free Python One-Liners Learning Resources
Python One-Line X - How to accomplish different tasks in a single line
Github '''Python One-Liners''' - Share your own one-liners with the community
Overview: 10 one-liners that fit into a tweet
I visited this page oftentimes and I loved studying the one-liners presented above. Thanks for creating this awesome resource, JAM, and RJW!
Because I learned a lot from studying the one-liners, I thought why not revive the page (after almost ten years since the last change happened)?
After putting a lot of effort into searching the web for inspiration, I created the following ten one-liners. Some of them are more algorithmic (e.g. Quicksort). Some day, I will add a detailed explanation here - but for now, you can read this blog article to find explanations.
1 # Palindrome Python One-Liner
2 phrase.find(phrase[::-1])
3
4 # Swap Two Variables Python One-Liner
5 a, b = b, a
6
7 # Sum Over Every Other Value Python One-Liner
8 sum(stock_prices[::2])
9
10 # Read File Python One-Liner
11 [line.strip() for line in open(filename)]
12
13 # Factorial Python One-Liner
14 reduce(lambda x, y: x * y, range(1, n+1))
15
16 # Performance Profiling Python One-Liner
17 python -m cProfile foo.py
18
19 # Power set Python One-Liner
20 lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])
21
22 # Fibonacci Python One-Liner
23 lambda x: x if x<=1 else fib(x-1) + fib(x-2)
24
25 # Quicksort Python One-liner
26 lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
27
28 # Sieve of Eratosthenes Python One-liner
29 reduce( (lambda r,x: r-set(range(x**2,n,x)) if (x in r) else r), range(2,int(n**0.5)), set(range(2,n)))
Find All Indices of an Element in a List
Say, you want to do the same as the list.index(element) method but return all indices of the element in the list rather than only a single one.
In this one-liner, you’re looking for element 'Alice' in the list lst = [1, 2, 3, 'Alice', 'Alice'] so it even works if the element is not in the list (unlike the list.index() method).
