Locionic
Blog
Projects
Dev ToolsCheatsheetsSeries & Guides
About MeFree Production KitContact
Send email to LocionicLocionic GitHub profileLocionic YouTube channel
© 2026Locionic• All rights reserved.
Dev Kits & Data•Tools•Tags•RSS•Privacy Policy•Terms
  1. Home
  2. Blog
  3. Python Tricks I Actually Reach For Daily
Thursday, April 23, 2026•13 min read

Python Tricks I Actually Reach For Daily

Locionic
Locionic(@Locionic)
python tricks tips productivity clean-code best-practices beginners
Python Tricks I Actually Reach For Daily
Python Tricks

Tuple Unpacking

Click to reveal
Python Tricks
Assign multiple values at once: `a, b = b, a`. Works for swaps, function returns, and dict entries. Zero overhead tuple creation.

Tuple Unpacking

Python Tricks

enumerate()

Click to reveal
Python Tricks
Loop with index and value: `for i, item in enumerate(items, start=1)`. Replaces error-prone `range(len(items))` pattern.

enumerate()

Python Tricks

zip()

Click to reveal
Python Tricks
Iterate multiple sequences in parallel: `for a, b in zip(list1, list2)`. Stops at shortest sequence by default.

zip()

Python Tricks

dict.get()

Click to reveal
Python Tricks
Safe dictionary access with default: `config.get('port', 8080)`. Returns default if key missing - no KeyError.

dict.get()

Python Tricks

Set lookup

Click to reveal
Python Tricks
Convert list to set for O(1) membership: `if item in allowed_set`. Much faster than list O(n) for repeated checks.

Set lookup

Python Tricks

Walrus operator

Click to reveal
Python Tricks
Assignment expression `:=` - assign and test in one: `if (n := len(items)) > 10: ...`. Avoids duplicate function calls.

Walrus operator

Python Tricks

dataclass

Click to reveal
Python Tricks
Decorator that auto-generates __init__, __repr__, __eq__: `@dataclass class Point: x: float; y: float`. Less boilerplate.

dataclass

Python Tricks

pathlib.Path

Click to reveal
Python Tricks
Object-oriented file paths: `base / 'config' / f'{env}.yaml'`. Cross-platform, chainable methods, reads/writes text directly.

pathlib.Path

Python Tricks

Forget the clever golfed code that reads like riddles. These are the Python features I reach for in production code - the ones that reduce boilerplate, prevent actual bugs, and make other developers nod in approval rather than scrolling to Stack Overflow.


Audio Briefing• Voice: Google Cloud Journey-F
0:00 / --:--

The Core 7 (with real context)

1. Tuple Unpacking for Swaps

No temp variable, no ceremony:

python
a, b = b, a

This creates a tuple on the right, then destructures it on the left. Works in-place, zero overhead. The same pattern handles multi-values:

python
# Swap three values in one line
i, j, k = j, k, i

# Unpack a function's multiple return values
x_coord, y_coord, label = get_point()

# Swap dict entries
d = {'a': 1, 'b': 2}
d['a'], d['b'] = d['b'], d['a']  # Now {'a': 2, 'b': 1}
Interactive Quiz

What does `a, b = b, a` actually do under the hood?

2. enumerate() - Index + Value

Stop doing for i in range(len(items)). It's error-prone and reads backwards:

python
# ❌ Old way - off-by-one bugs hiding here
for i in range(len(colors)):
    print(i, colors[i])

# ✅ Python way - what you meant
for i, color in enumerate(colors):
    print(i, color)
Pro move: start at 1

enumerate(colors, start=1) gives 1-based numbering - useful for display, error messages, test cases.

Interactive Quiz

What's the benefit of `enumerate(items, start=1)`?

3. zip() - Loop Two Sequences in Parallel

Cleaner than manual index management when two sequences must stay in sync:

python
names    = ["Alice", "Bob", "Charlie"]
scores   = [85, 92, 78]
subjects = ["Math", "Science", "English"]

# Pairing two lists
for name, score in zip(names, scores):
print(f"{name}: {score}")

# Three-way zipping (tuples auto-unpacked)
for name, score, subject in zip(names, scores, subjects):
print(f"{name} ({subject}): {score}")

# Dictionaries from two lists
scores_dict = dict(zip(names, scores))
# {'Alice': 85, 'Bob': 92, 'Charlie': 78}
zip truncates to the shortest

If names has 3 items and scores has 5, only 3 pairs are produced. Use itertools.zip_longest if you want padding behavior.

Interactive Quiz

What happens when you zip two lists of different lengths?

4. dict.get() - Avoid KeyError

The difference between "missing key expected" and "missing key is a bug":

python
config = {"port": 3000, "host": "localhost"}

# Returns default if key missing - no exception
port     = config.get("port", 8080)     # 3000
debug    = config.get("debug", False)   # False (key missing)
host     = config.get("host", "0.0.0.0") # "localhost"

# None is the implicit default
value = config.get("nonexistent")       # None
ScenarioUseReason
Required config, missing = bug
<code>config["key"]</code>
Fail fast, surface error
Optional key, default OK
<code>config.get("key", default)</code>
Graceful fallback
Need to distinguish missing from None
<code>if "key" in config</code>
None is a valid value
Interactive Quiz

When should you use `config["key"]` instead of `config.get("key", default)`?

5. Set Lookups for O(1) Membership

Converting a list to a set before checking membership is one of the most common "hidden slowness" fixes:

python
allowed_actions = {"read", "write", "delete", "update"}

# O(1) - constant time regardless of list size
if action in allowed_actions:
execute(action)

# ❌ With a list, this is O(n) - scales poorly
allowed_actions_list = ["read", "write", ...]
if action in allowed_actions_list:  # Slower as list grows
execute(action)
Rule of thumb

If x in collection runs more than a handful of times per request and the collection has more than ~10 items, it should almost certainly be a set.

Interactive Quiz

Why is `item in some_set` faster than `item in some_list` for large collections?

6. Underscore `_` for Intentional Ignoring

Communicates "I'm not using this value on purpose":

python
# Unpacking - ignore the key, keep the value
_, result = parse_line(line)

# Loop - ignore the index
for _ in range(3):
print("repeat")

# Match with explicit wildcard (Python 3.10+)
match status:
case 200:
    handle_ok()
case 404:
    handle_missing()
case _:
    handle_other()  # "anything else"

# Unpacking partial - keep start/end, ignore middle
first, *_, last = [1, 2, 3, 4, 5]
# first=1, last=5
Interactive Quiz

What does `first, *_, last = [1, 2, 3, 4, 5]` produce?


Deep-Cut Techniques (Actually Useful)

python
# Without walrus
data = fetch_record()
if data:
process(data)

# With walrus :=
if data := fetch_record():
process(data)
When NOT to use walrus

Don't force it into existing clean code. If the assignment belongs outside the condition, put it there. The walrus shines in: loop guards, catch blocks, and reducing duplicated function calls.

Interactive Quiz

When is the walrus operator `:=` most valuable?

Python 3.12 added f-string debugging with = and multiline f-strings:

python
name, age = "Alice", 30

# Debug mode | shows expression + value
print(f"{name=} {age=}")  # name='Alice' age=30

# Multiline (no more backslash continuation)
report = f"""
Name:   {name}
Status: {"Adult" if age >= 18 else "Minor"}
Score:  {95.5:.1f}%
"""
Interactive Quiz

What does `f"{name=} {age=}"` output when name="Alice" and age=30?

Stop concatenating with os.path.join():

python
from pathlib import Path

base = Path("/app/data")

# Fluent chaining | works cross-platform
config = base / "config" / f"env_{ENV}.yaml"
logs   = base / "logs" / "app.log"

# Read/write without manual open()
text = config.read_text(encoding="utf-8")
config.write_text(new_text, encoding="utf-8")

# Existence checks
if config.exists() and config.is_file():
size = config.stat().st_size

# Iterate directory
for log_file in base.glob("**/*.log"):
print(log_file.name, log_file.stat().st_size)
Why pathlib over os.path?

Path uses operator overloading (/ joins paths) which reads naturally. Methods are chainable. The same code works on Windows and Linux without changes.

Interactive Quiz

What does `Path('/app') / 'data' / 'config.yaml'` produce?

Before dataclasses, classes with many attributes needed verbose init, repr, and eq. Dataclasses generate those automatically:

python
from dataclasses import dataclass, field
from typing import List

@dataclass
class Project:
name: str
slug: str
tags: List[str] = field(default_factory=list)
starred: bool = False

# ✅ Auto-generated __init__, __repr__, __eq__
p = Project(
name="Portfolio",
slug="portfolio",
tags=["nextjs", "tailwind"],
starred=True,
)
print(p)  # Project(name='Portfolio', slug='portfolio', ...)

# ✅ Frozen dataclass = immutable (no keyword mutability issues)
from dataclasses import dataclass

@dataclass(frozen=True)
class Point:
x: float
y: float

pt = Point(1.0, 2.0)
# pt.x = 3.0  # FrozenInstanceError
FeatureRegular ClassDataclass
__init__
Manual boilerplate
Auto-generated
__repr__
Manual or ugly default
Clean: Class(field=value, ...)
__eq__
Identity only (unless manual)
Value equality
Immutability
Manual property setters
@dataclass(frozen=True)
Interactive Quiz

What does `@dataclass(frozen=True)` give you that a regular class doesn't?

Combine defaultdict with context managers for clean resource handling:

python
from collections import defaultdict
from contextlib import contextmanager
from threading import Lock

# Grouping | defaultdict eliminates "if key in dict" checks
grouped = defaultdict(list)
for item in items:
grouped[item.category].append(item)

# Custom context manager for timing
@contextmanager
def timer(label):
import time
start = time.perf_counter()
yield
elapsed = time.perf_counter() - start
print(f"{label}: {elapsed*1000:.2f}ms")

with timer("database query"):
results = db.query("SELECT * FROM users")
Interactive Quiz

What does `defaultdict(list)` eliminate?


Decision Reference

ProblemSolutionWhy
Avoid KeyError on optional config
<code>dict.get(key, default)</code>
Graceful fallback, no try/except
Loop with index + value
<code>enumerate(items)</code>
Eliminates range(len(...)) anti-pattern
Pair two sequences
<code>zip(a, b)</code>
No manual index state
Fast membership check
<code>set</code>
O(1) vs O(n) for lists
Assign + test in one line
<code>:=</code> walrus
Remove duplicate function calls
Group by key
<code>defaultdict(list)</code>
No if key not in dict boilerplate
Typed record with methods
<code>@dataclass</code>
Auto-init/eq/repr, less boilerplate
Cross-platform paths
<code>pathlib.Path</code>
Fluent API, chainable

TL;DR

Start with these today
Minimum viable skill stack:
  • Use enumerate() and zip() - they eliminate 80% of loop bugs
  • Use dict.get() for optional configs - fail only where it's a real error
  • Use _ consistently - it signals intent to humans and linters
  • Profile before optimizing, but profile sets first - they're the fastest free win
Interactive Quiz

You have a list of 50,000 allowed actions and check `action in allowed_actions` 100 times per request. What's the single best optimization?

Python Tips Productivity
Related Posts
  • Python Fundamentals You Actually Need - Start here if you're new to Python
  • Modern Python Development Environment - Set up pyenv, Poetry, uv, and pyproject.toml
  • Python Practice: Automation Scripts - Real-world automation examples

You Might Also Like

  • A Practical pytest Tutorial That Actually Fixes Early Gotchas
  • Advanced Pytest Fixtures and Parameterization Patterns
  • Benchmarking Redis Rate Limiters: Token Bucket vs. Sliding Window in Python
  • Real-Time Data Streaming with Apache Kafka: A Production Guide
Advertisement
Share this article:

Stay Updated

Get the latest posts delivered straight to your inbox.

Free Developer Utilities

Free In-Browser Developer Tools

Clean AI CLI logs, build cron expressions, decode JWTs, and calculate chmod permissions offline.

Explore Tools

Related Articles

The Python Fundamentals You Actually Need to Start
pythonApril 13, 2026

The Python Fundamentals You Actually Need to Start

Master foundational Python paradigms: memory references, mutable vs immutable types, scoping rules, generators, and clean object-oriented patterns.

Read more
Python's Underscores: Conventions, Not Access Modifiers
pythonApril 20, 2026

Python's Underscores: Conventions, Not Access Modifiers

Python uses underscores as naming conventions, not enforced access modifiers - understanding the difference prevents confusing code and helps you write better APIs.

Read more
A Practical pytest Tutorial That Actually Fixes Early Gotchas
pythonApril 23, 2026

A Practical pytest Tutorial That Actually Fixes Early Gotchas

Master pytest for production Python: fixtures, parameterization, monkeypatch mocking, custom markers, and clean test suite architecture.

Read more
On this page
0%
  • The Core 7 (with real context)
  • Swap three values in one line
  • Unpack a function's multiple return values
  • Swap dict entries
  • Pairing two lists
  • Three-way zipping (tuples auto-unpacked)
  • Dictionaries from two lists
  • {'Alice': 85, 'Bob': 92, 'Charlie': 78}
  • Returns default if key missing - no exception
  • None is the implicit default
  • O(1) - constant time regardless of list size
  • ❌ With a list, this is O(n) - scales poorly
  • Unpacking - ignore the key, keep the value
  • Loop - ignore the index
  • Match with explicit wildcard (Python 3.10+)
  • Unpacking partial - keep start/end, ignore middle
  • first=1, last=5
  • Deep-Cut Techniques (Actually Useful)
  • Without walrus
  • With walrus :=
  • Debug mode | shows expression + value
  • Multiline (no more backslash continuation)
  • Fluent chaining | works cross-platform
  • Read/write without manual open()
  • Existence checks
  • Iterate directory
  • ✅ Auto-generated init, repr, eq
  • ✅ Frozen dataclass = immutable (no keyword mutability issues)
  • pt.x = 3.0 # FrozenInstanceError
  • Grouping | defaultdict eliminates "if key in dict" checks
  • Custom context manager for timing
  • Decision Reference
  • TL;DR
PreviousHow to Start Writing Python Code Today (A No-Fluff Guide)Read NextPython Libraries I'd Bet On for 2026