Welcome to DRIXO — Your Coding Journey Starts Here
DRIXO Code • Learn • Build

10 Python One-Liners That Will Impress Your Friends

February 01, 2026 8 min read 0 Comments
10 Python One-Liners That Will Impress Your Friends
Python

10 Python One-Liners That Will Impress Your Friends

DRIXO

Code · Learn · Build

When I first learned about 10 python one-liners that will impress your friends, I made every mistake possible. This tutorial is the guide I wish I had — clear, practical, and filled with real code.

List and Dict Tricks

These one-liners will make your code shorter and more Pythonic:

# 1. Swap two variables
a, b = 1, 2
a, b = b, a  # a=2, b=1

# 2. Flatten a nested list
nested = [[1, 2], [3, 4], [5, 6]]
flat = [x for sub in nested for x in sub]  # [1, 2, 3, 4, 5, 6]

# 3. Reverse a string
text = "Hello DRIXO"
reversed_text = text[::-1]  # "OXIRD olleH"

# 4. Count word frequency
words = "the cat sat on the mat the cat".split()
freq = {w: words.count(w) for w in set(words)}
# {'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1}

# 5. Merge two dictionaries
defaults = {'theme': 'dark', 'lang': 'en', 'font_size': 14}
user_prefs = {'theme': 'light', 'font_size': 18}
config = {**defaults, **user_prefs}
# {'theme': 'light', 'lang': 'en', 'font_size': 18}

Filtering and Transformation

# 6. Filter with condition
numbers = range(-5, 6)
positives = [n for n in numbers if n > 0]  # [1, 2, 3, 4, 5]

# 7. Find most common element
from collections import Counter
data = [1, 3, 2, 1, 5, 3, 1, 2]
most_common = Counter(data).most_common(1)[0][0]  # 1

# 8. Transpose a matrix
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed = list(zip(*matrix))
# [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

# 9. Check if string is palindrome
is_palindrome = lambda s: s.lower() == s.lower()[::-1]
print(is_palindrome("racecar"))  # True

# 10. Get unique items preserving order
items = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
unique = list(dict.fromkeys(items))  # [3, 1, 4, 5, 9, 2, 6]

Advanced One-Liners

# 11. Read file into list of lines
lines = open('data.txt').read().splitlines()

# 12. Create a simple HTTP server (run in terminal)
# python -m http.server 8000

# 13. Chained comparison
x = 15
result = 10 < x < 20  # True (same as: 10 < x and x < 20)

# 14. Conditional assignment
status = "active" if score > 50 else "inactive"

# 15. Multiple assignment from iterable
first, *middle, last = [1, 2, 3, 4, 5]
# first=1, middle=[2, 3, 4], last=5

# 16. Dictionary from two lists
keys = ['name', 'age', 'city']
values = ['Alice', 28, 'NYC']
person = dict(zip(keys, values))
# {'name': 'Alice', 'age': 28, 'city': 'NYC'}

# 17. Remove duplicates from sorted list
sorted_nums = [1, 1, 2, 2, 3, 3, 4]
unique_sorted = list(set(sorted_nums))  # [1, 2, 3, 4]

# 18. Generate a random password
import string, random
password = ''.join(random.choices(string.ascii_letters + string.digits, k=16))

When NOT to Use One-Liners

One-liners are cool, but readability matters more. Don't use them when:

  • The logic is complex with multiple conditions
  • Error handling is needed
  • Other developers need to maintain the code
  • Performance is critical (some one-liners are slower)
# BAD: Too clever
result = {k: [x for x in v if x > 0] for k, v in data.items() if len(v) > 2}

# GOOD: Clear and maintainable
result = {}
for key, values in data.items():
    if len(values) > 2:
        positive_values = [x for x in values if x > 0]
        result[key] = positive_values
Pro Tip: The Zen of Python says: 'Readability counts.' Use one-liners for simple operations, but expand complex logic into multiple lines.

Practice Challenges

Try writing one-liners for these:

# Challenge 1: FizzBuzz in one line
fizzbuzz = ['FizzBuzz' if i%15==0 else 'Fizz' if i%3==0 else 'Buzz' if i%5==0 else i for i in range(1, 101)]

# Challenge 2: Fibonacci sequence
fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)

# Challenge 3: Caesar cipher
encrypt = lambda text, shift: ''.join(chr((ord(c) - 97 + shift) % 26 + 97) if c.isalpha() else c for c in text.lower())
print(encrypt("hello", 3))  # "khoor"

# Challenge 4: Check if number is prime
is_prime = lambda n: n > 1 and all(n % i != 0 for i in range(2, int(n**0.5) + 1))
print([n for n in range(2, 50) if is_prime(n)])
AM
Arjun Mehta
Full-Stack Developer & Technical Writer at DRIXO

Full-stack developer with 5+ years of experience in Python and JavaScript. I love breaking down complex concepts into simple, practical tutorials. When I'm not coding, you'll find me contributing to open-source projects.

Comments