🧠 Why One-Liners Matter:
ne-liners help you:
Avoid writing 5 lines when 1 is enough
Think in a “Pythonic” way
Impress interviewers and fellow coders
Speed up problem-solving in competitive coding
🚀 Let’s Jump In!
Swap Two Variables (No Temp)
a, b = 10, 20
a, b = b, a
No need for a third variable.
Output: a = 20, b = 10
Reverse a String
rev = "python"[::-1]
Output: 'nohtyp'
Check If a Number is Even
is_even = lambda x: x % 2 == 0
Usage: is_even(4) → True
Get All Even Numbers in a List
evens = [x for x in range(20) if x % 2 == 0]
Output: [0, 2, 4, 6, ..., 18]
Flatten a 2D List
flat = [item for sublist in [[1,2], [3,4]] for item in sublist]
Output: [1, 2, 3, 4]
Count Frequency of Items in a List
from collections import Counter
freq = Counter(["a", "b", "a", "c", "b", "a"])
Output: {'a': 3, 'b': 2, 'c': 1}
Find Factorial in One Line
from math import factorial
print(factorial(5)) # Output: 120
Get Unique Items From a List
unique = list(set([1, 2, 2, 3, 3, 4]))
Output: [1, 2, 3, 4]
Check if a String is a Palindrome
is_palindrome = lambda s: s == s[::-1]
Usage: is_palindrome("madam") → True
Print a Table in One Line
print('\n'.join([f"{i} x 5 = {i*5}" for i in range(1, 11)]))
Output:
1 x 5 = 5
2 x 5 = 10
...
10 x 5 = 50
✅ Final Thoughts: These one-liners may look simple, but they teach you powerful concepts like:
List comprehensions
Lambda functions
Built-in libraries
Pythonic patterns