10 Python One-Liners That Do the Work of 5 Lines

Why write five lines when Python lets you do it in one — without sacrificing readability?

10 Python One-Liners That Do the Work of 5 Lines
Photo by Dan Loftus on Unsplash

Write less. Do more. Look like a wizard doing it.

10 Python One-Liners That Do the Work of 5 Lines

If you’ve ever written five lines of Python code and wondered, “There has to be a shorter way,” you’re not alone. Python is known for its expressiveness — and in the hands of someone who knows the language well, it can often do in one line what takes five in other languages.

In this article, we’ll explore 10 Python one-liners that are not only shorter, but also more elegant and Pythonic. These snippets can help you write cleaner, more maintainable code (and impress your peers).


1. Swap Two Variables

Typical Way:

temp = a 
a = b 
b = temp

One-Liner:

a, b = b, a
Python makes swapping variables as simple as breathing. No need for a temporary variable!

2. Read a File Into a List

Typical Way:

lines = [] 
with open("file.txt") as f: 
    for line in f: 
        lines.append(line.strip())

One-Liner:

lines = [line.strip() for line in open("file.txt")]
List comprehensions + file objects = cleaner, concise file reading.

3. Flatten a List of Lists

Typical Way:

flattened = [] 
for sublist in nested: 
    for item in sublist: 
        flattened.append(item)

One-Liner:

flattened = [item for sublist in nested for item in sublist]
Double for in a list comprehension? Python magic.

4. Get the Frequency of Each Item in a List

Typical Way:

freq = {} 
for item in items: 
    if item in freq: 
        freq[item] += 1 
    else: 
        freq[item] = 1

One-Liner:

from collections import Counter 
freq = Counter(items)
The Counter class handles all the heavy lifting—clean and built for this task.

5. Find the Intersection of Two Lists

Typical Way:

intersection = [] 
for item in list1: 
    if item in list2: 
        intersection.append(item)

One-Liner:

intersection = list(set(list1) & set(list2))
Using set intersection is not only shorter but also much faster for large lists.

6. Check if All Elements Satisfy a Condition

Typical Way:

all_positive = True 
for num in nums: 
    if num <= 0: 
        all_positive = False 
        break

One-Liner:

all_positive = all(num > 0 for num in nums)
Python’s built-in all() is a perfect fit here—clean, readable, efficient.

7. Get the Most Frequent Element

Typical Way:

counts = {} 
for item in data: 
    counts[item] = counts.get(item, 0) + 1 
max_item = max(counts, key=counts.get)

One-Liner:

from collections import Counter 
max_item = Counter(data).most_common(1)[0][0]
No loops, no manual counting — just one line of truth.

8. Reverse a String

Typical Way:

reversed_str = "" 
for char in original: 
    reversed_str = char + reversed_str

One-Liner:

reversed_str = original[::-1]
Python slices let you time-travel. This is how you reverse a string like a pro.

9. Generate Fibonacci Numbers

Typical Way:

def fib(n): 
    seq = [0, 1] 
    for i in range(2, n): 
        seq.append(seq[-1] + seq[-2]) 
    return seq

One-Liner:

fib = lambda n: [0, 1][:n] if n < 2 else [0, 1] + [sum(pair) for pair in zip([0, 1] + [0]*(n-2), fib(n-1))]
Clever, but use responsibly — readability counts too. Lambda recursion has its place (but maybe not in production).

10. Get a Dictionary From Two Lists

Typical Way:

d = {} 
for i in range(len(keys)): 
    d[keys[i]] = values[i]

One-Liner:

d = dict(zip(keys, values))
Python’s zip() and dict() turn parallel lists into elegant key-value pairs.

Final Thoughts

These one-liners showcase the expressive power of Python. But remember: just because you can write it in one line doesn’t mean you should. Readability matters. Use these wisely to simplify your code — not to confuse your future self or your teammates.

But when you find the right balance? Your Python code becomes clean, Pythonic, and just plain fun to write.


Found these one-liners useful? Share your favorite Python trick in the comments or give this post a 👏 to help others write better code too.

Happy coding! 🐍

Photo by Tirza van Dijk on Unsplash