Member-only story
30 Mind-Blowing Python One-Liners You Need to Try! 🚀
4 min readFeb 20, 2025
Python is famous for its simplicity and elegance, and one-liners are a perfect example of this.
Here are 30 powerful Python one-liners that will save time, boost productivity, and impress your colleagues!
1. Reverse a string
print("Python"[::-1]) # Output: 'nohtyP'
2. Check if a string is a palindrome
is_palindrome = lambda s: s == s[::-1]
print(is_palindrome("madam")) # Output: True
3. Flatten a nested list
flat_list = lambda lst: [item for sublist in lst for item in sublist]
print(flat_list([[1, 2], [3, 4], [5]])) # Output: [1, 2, 3, 4, 5]
4. Find the most frequent element in a list
from collections import Counter
most_frequent = lambda lst: Counter(lst).most_common(1)[0][0]
print(most_frequent([1, 3, 2, 1, 4, 1, 3])) # Output: 1
5. Swap two variables without a temporary variable
a, b = 5, 10
a, b = b, a
print(a, b) # Output: 10 5
6. Remove duplicates from a list
unique = lambda lst: list(set(lst))…