12 Must‑Know Python One‑Liners to Boost Your Coding Efficiency
This article presents twelve powerful Python one‑liners—from compact for loops and ternary expressions to lambda functions, tuple swapping, list comprehensions, and dictionary creation—each explained with concise examples that demonstrate how to write common tasks in a single line of code.
Python is a popular programming language because its concise syntax makes coding enjoyable. This article introduces twelve impressive Python one‑liners that can save time and reduce code length.
1. One‑Line For Loop
This one‑liner lets you write a for loop on a single line, printing each element of a list.
# example code
mylist = [4, 6, 8, 1, 3]
for x in mylist: print(x)
# Output
4
6
8
1
32. One‑Line If‑Else (Ternary Operator)
Use Python’s ternary operator to evaluate a condition and return one of two values in a single expression.
# example code 1
myval = 24 if 5 < 2 else 40
print(myval) # 40
# example code 2
myval = 22 if 3 < 6 else 80
print(myval) # 223. One‑Line Function with lambda
Define quick anonymous functions using lambda, which can be called like regular functions.
# example
fun = lambda x: x % 2
print(fun(3)) # 1 / False
print(fun(2)) # 0 / True4. One‑Line Variable Swapping
Swap two variables without a temporary placeholder using tuple unpacking.
# old method
v1 = 1
v2 = 2
temp = v1
v1 = v2
v2 = temp
print(v1, v2) # 2 1
# new method
v1 = 1
v2 = 2
v1, v2 = v2, v1
print(v1, v2) # 2 15. One‑Line List Summation
Use the built‑in sum function to add all elements of a list in a single line.
# example
mylist = [1, 2, 3, 4, 5]
print(sum(mylist)) # 156. One‑Line Nested Loops (Double Loop)
Compress a nested for loop into a single line using list comprehension.
# multi‑line loop
lst1 = [1, 2]
lst2 = ["x", "y"]
for x in lst1:
for y in lst2:
print(x, y)
# one‑line version
[print(x, y) for x in lst1 for y in lst2]7. One‑Line Sorting
Sort a list of numbers or strings in one line with the sorted function.
# integer sorting
mylist = [5, 3, 1, 0, 2, 4]
print(sorted(mylist)) # [0, 1, 2, 3, 4, 5]
# string sorting
mylist = ["C", "A", "B"]
print(sorted(mylist)) # ['A', 'B', 'C']8. One‑Line Multiple Assignment
Assign values to several variables simultaneously in a single statement.
# multi‑line method
a = 1
b = 2
c = 3
print(a, b, c) # 1 2 3
# one‑line method
a, b, c = 1, 2, 3
print(a, b, c) # 1 2 39. One‑Line Palindrome Check
Check whether a string or number is a palindrome using slicing.
# example
val = '121'
isPalindrome = val == val[::-1]
print(isPalindrome) # True10. One‑Line List Filtering (List Comprehension)
Filter elements of a list with a condition using list comprehension.
# example
mylist = [2, 4, 9, 10, 1, 11, 34, 56]
result = [x for x in mylist if x > 8]
print(result) # [9, 10, 11, 34, 56]11. One‑Line Dictionary Creation with enumerate
Create a dictionary that maps indices to list elements using enumerate.
# example
mylist = ["a", "b", "c", "d"]
mydict = dict(enumerate(mylist))
print(mydict) # {0: 'a', 1: 'b', 2: 'c', 3: 'd'}12. One‑Line String‑to‑Integer Conversion
Convert a list of numeric strings to integers using map.
# example
strlist = ["1", "2", "3", "4", "5"]
intlist = list(map(int, strlist))
print(intlist) # [1, 2, 3, 4, 5]These one‑liners showcase Python’s ability to express common tasks succinctly, helping developers write cleaner and more efficient code.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
