10 Python One-Liners That Will Save You Tons of Time

Oliver Bennet
3 min readOct 17, 2024

--

Python is known for its simplicity and elegance, and nothing shows that better than Python one-liners! These little snippets of code pack a lot of power into a single line and can often save you from writing entire functions or loops. Let’s dive into 10 useful Python one-liners that every developer should know.

1. Swapping Variables

x, y = y, x

Instead of using a temporary variable to swap values, this one-liner makes your code cleaner and more Pythonic.

2. Reversing a String

reversed_string = my_string[::-1]

With Python’s slicing feature, you can reverse a string in just one line. Easy, right?

3. List Comprehension for Creating Lists

squares = [x**2 for x in range(10)]

This one-liner creates a list of squared numbers from 0 to 9. List comprehensions are an essential part of writing cleaner Python.

4. Flatten a List of Lists

flattened = [item for sublist in my_list for item in sublist]

If you’ve ever had a nested list, you know how useful it is to flatten it out. This one-liner helps you do just that.

5. Find the Intersection of Two Lists

common_elements = set(list1) & set(list2)

Working with sets makes it easy to find common elements between two lists.

6. Read a File into a List of Lines

lines = open('file.txt').read().splitlines()

This reads a file into a list where each line is an element, while avoiding the newline characters.

7. Check for Palindrome

is_palindrome = lambda s: s == s[::-1]

A quick lambda function to check if a string is a palindrome. No need for loops or extra variables.

8. Find the Most Frequent Element in a List

most_frequent = max(set(my_list), key=my_list.count)

This one-liner identifies the most frequent element in a list by leveraging Python’s max() function and count().

9. Merge Two Dictionaries

merged_dict = {**dict1, **dict2}

In Python 3.5 and above, merging dictionaries is as simple as this. It’s clean, concise, and super useful.

10. FizzBuzz in One Line

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

FizzBuzz, a popular programming problem, can be done in one line using list comprehension and conditional expressions.

Wrapping Up

Python’s flexibility lets you reduce complex tasks into simple, readable one-liners. These snippets can help you write cleaner code and, at the same time, make you look like a Python wizard. Know any other useful Python one-liners? Drop them in the comments below!

👉 How to Install Docker on Ubuntu:

🔗 Related Links:

▶️ Explore more open-source tutorials on my website: https://graphpe.com

▶️ Follow me on X for updates: https://x.com/obennetgpe

▶️ Check out my articles on dev.to: https://dev.to/oliverbennet

☕ Buy me a Coffee Here: https://buymeacoffee.com/graphpe

--

--

Oliver Bennet
Oliver Bennet

Written by Oliver Bennet

20 Years of Open-Source Experience. Currently I Write about DevOps, Programming and Linux Technologies.

No responses yet