DEV Community

Alex Spinov
Alex Spinov

Posted on

10 Python One-Liners That Will Make You Look Like a Senior Developer

I've been reviewing code for years. The difference between junior and senior Python isn't frameworks — it's knowing the language's built-in power.

Here are 10 one-liners I use daily that consistently impress in code reviews.

1. Flatten a Nested List

# Junior
flat = []
for sublist in nested:
    for item in sublist:
        flat.append(item)

# Senior
flat = [item for sub in nested for item in sub]

# Even shorter (if you need it truly flat at any depth)
import itertools
flat = list(itertools.chain.from_iterable(nested))
Enter fullscreen mode Exit fullscreen mode

2. Count Occurrences

# Junior
counts = {}
for item in data:
    counts[item] = counts.get(item, 0) + 1

# Senior
from collections import Counter
counts = Counter(data)
# Bonus: counts.most_common(3) gives top 3
Enter fullscreen mode Exit fullscreen mode

3. Merge Two Dictionaries

# Junior
merged = dict1.copy()
merged.update(dict2)

# Senior (Python 3.9+)
merged = dict1 | dict2

# Or (Python 3.5+)
merged = {**dict1, **dict2}
Enter fullscreen mode Exit fullscreen mode

4. Read a File into a List (Stripped)

# Junior
lines = []
with open('file.txt') as f:
    for line in f:
        lines.append(line.strip())

# Senior
lines = Path('file.txt').read_text().splitlines()
Enter fullscreen mode Exit fullscreen mode

5. Find the Most Common Element

# Junior
from collections import Counter
counter = Counter(data)
most_common = counter.most_common(1)[0][0]

# Senior
most_common = max(set(data), key=data.count)

# Actually fastest for large data:
most_common = Counter(data).most_common(1)[0][0]
Enter fullscreen mode Exit fullscreen mode

6. Transpose a Matrix

# Junior
transposed = []
for i in range(len(matrix[0])):
    row = []
    for j in range(len(matrix)):
        row.append(matrix[j][i])
    transposed.append(row)

# Senior
transposed = list(zip(*matrix))
Enter fullscreen mode Exit fullscreen mode

7. Remove Duplicates While Preserving Order

# Junior
seen = set()
unique = []
for item in data:
    if item not in seen:
        seen.add(item)
        unique.append(item)

# Senior
unique = list(dict.fromkeys(data))
Enter fullscreen mode Exit fullscreen mode

8. Conditional Assignment (Walrus Operator)

# Junior
result = expensive_function()
if result:
    process(result)

# Senior (Python 3.8+)
if result := expensive_function():
    process(result)
Enter fullscreen mode Exit fullscreen mode

9. Group a List by Key

# Junior
groups = {}
for item in data:
    key = item['category']
    if key not in groups:
        groups[key] = []
    groups[key].append(item)

# Senior
from itertools import groupby
from operator import itemgetter
groups = {k: list(v) for k, v in groupby(sorted(data, key=itemgetter('category')), key=itemgetter('category'))}

# Actually cleaner:
from collections import defaultdict
groups = defaultdict(list)
for item in data:
    groups[item['category']].append(item)
Enter fullscreen mode Exit fullscreen mode

10. Quick HTTP Request + JSON Parse

# Junior
import urllib.request
import json
response = urllib.request.urlopen('https://api.example.com/data')
data = json.loads(response.read().decode())

# Senior
import requests
data = requests.get('https://api.example.com/data').json()

# No dependencies (Python 3.x):
from urllib.request import urlopen
data = json.loads(urlopen('https://api.example.com/data').read())
Enter fullscreen mode Exit fullscreen mode

The Real Senior Move

The actual senior developer skill isn't knowing all of these — it's knowing when to use them.

A one-liner that nobody on your team understands is worse than 5 readable lines. The best code is the code that your future self (and your teammates) can read at 2 AM during an incident.

My rule: Use one-liners for common patterns everyone knows (dict.fromkeys, Counter, zip(*matrix)). Write explicit code for business logic.


What's your favorite Python one-liner? I'm always looking for new ones.

For more Python tools: check out my 145+ open-source repos.

Follow for more Python and developer productivity content.

Top comments (0)