10 Actually Useful Python One-Liners (2026)
You know print, len, and for loops. This list covers the Python one-liners that experienced developers reach for when they need to manipulate data structures, inspect systems, or serve files — all in a single line. No imports beyond the standard library required.
The One-Liners
Flatten a nested list — Turns [[1, 2], [3, 4, 5], [6]] into [1, 2, 3, 4, 5, 6].
flat = [item for sublist in nested for item in sublist]Output: [1, 2, 3, 4, 5, 6]. For deeply nested lists, use from itertools import chain; list(chain.from_iterable(nested)).
Transpose a matrix — Swaps rows and columns in a 2D list.
transposed = list(zip(*matrix))Output: zip(*matrix) unpacks rows as arguments to zip, which pairs first elements, then second, etc. Input [[1,2],[3,4],[5,6]] → [(1,3,5),(2,4,6)].
Find the most common element — Uses Counter to count occurrences and return the most frequent item.
from collections import Counter; most_common = Counter(items).most_common(1)[0][0]Output: The most frequent element in the list. most_common(1) returns [(element, count)], so [0][0] extracts just the element.
Chunk a list into batches — Splits a list into equal-sized chunks for batch processing.
chunks = [lst[i:i+n] for i in range(0, len(lst), n)]Output: [[1,2,3], [4,5,6], [7,8]] for lst=[1,2,3,4,5,6,7,8] and n=3. Essential for batch API calls, database inserts, or progress bars.
Merge two dicts with unpacking — Combines two dictionaries, with the second dict’s values winning conflicts.
merged = {**dict1, **dict2}Output: A new dict with all keys from both. For Python 3.9+, use merged = dict1 | dict2. For nested merging, use collections.ChainMap.
Remove None and empty values from a list — Filters out falsy values like None, 0, "", and [].
cleaned = [x for x in items if x]Output: Removes all falsy values. For removing only None (keeping 0 and ""): [x for x in items if x is not None].
Check if a string is a palindrome — Tests whether a string reads the same forward and backward.
is_palindrome = lambda s: s == s[::-1]Output: True for “racecar”, “madam”. Case-insensitive: lambda s: s.lower() == s.lower()[::-1].
Transpose dict of lists to list of dicts — Converts column-oriented data to row-oriented.
rows = [dict(zip(data, t)) for t in zip(*data.values())]Output: data = {"name": ["a","b"], "age": [1,2]} → [{"name":"a","age":1}, {"name":"b","age":2}]. Perfect for converting API responses or CSV column data.
Run an external command and capture output — Executes a shell command and captures stdout.
import subprocess; output = subprocess.check_output(["ls", "-l"], text=True)Output: The directory listing as a string. Use subprocess.run with capture_output=True for more control (stderr capture, timeout, error handling).
Create a simple HTTP server — Serves the current directory over HTTP in one line.
python3 -m http.server 8080Output: A web server on port 8080 serving static files from the current directory. Use --bind 0.0.0.0 to make it accessible from other machines. Never use in production — it’s single-threaded and has no security.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro