Trey Hunner
banner
trey.io
Trey Hunner
@trey.io
Python & Django team trainer

I help folks sharpen their Python skills with https://PythonMorsels.com 🐍🍪

YIMBY. 95% vegan.
Python Tip #20 (of 365):

Don't use "range(len(...))" 🧵

If you find yourself using "range(len(...))" in a loop, always pause to ask "is there a better way to do this?"

There usually is.

#Python #DailyPythonTip
January 20, 2026 at 7:04 PM
Boosting this because this is a good offer if you're interested. Philip is someone I'd trust to help me with Mastodon questions.
Hey if you’re looking to try/retry/find a home on the ActivityPub/mastodon shaped part of the fediverse, I will personally help you get set up, move followers, block assholes, etc

I have no interest in debating the merit of bsky-v-mastodon, purely offering help.

Reskeets welcome ✨
January 20, 2026 at 3:01 AM
Unlike f-strings, t-strings don't actually make strings. Read more �‘‰ https://trey.io/n4mlgb #Python
January 20, 2026 at 1:27 AM
Python Tip #19 (of 365):

Prefer "for" loops over "while" loops. 🧵

Instead of this:

n = 1
while n < 10:
print(n)
n += 1

Prefer this:

for n in range(1, 10):
print(n)

When you're considering a "while" loop, ask whether there's a way to use "for" loop instead.

#Python #DailyPythonTip
January 19, 2026 at 7:04 PM
Python Tip #18 (of 365):

Merge iterables using asterisks, like this: "[*first, *second]" 🧵

To merge two iterables, first and second, into third don't do this:

third = list(first) + list(second)

Do this:

third = [*first, *second]

Why?...

#Python #DailyPythonTip
January 18, 2026 at 7:04 PM
Python Tip #17 (of 365):

Avoid using the "in" operator on lists... especially large lists! 🧵

A containment check (using the "in" operator) on a list requires looping over the list.

To repeatedly check whether something is contained in a list, consider using a set instead.

#Python #DailyPythonTip
January 17, 2026 at 7:04 PM
Python Tip #16 (of 365):

To pre-fill lists with immutable values, use self-concatenation. 🧵

#Python #DailyPythonTip
January 16, 2026 at 2:03 PM
Reposted by Trey Hunner
This is how free and open access works. There is no way to have a free and open access project and then go “wait, no, not like that”.

www.citationneeded.news/free-and-ope...
January 15, 2026 at 7:18 PM
Reposted by Trey Hunner
Meet Joanne, who noticed an influential historical woman was missing on Wikipedia and took action #Wikipedia25
January 16, 2026 at 1:14 AM
Python Tip #15 (of 365):

Don't forget about slicing 🧵

To get all command-line arguments but the first, you COULD do this:

import sys
arguments = list(sys.argv)
arguments.pop(0)

But slicing is better for "get all but the first":

import sys
arguments = sys.argv[1:]

#Python #DailyPythonTip
January 15, 2026 at 7:04 PM
Python Tip #14 (of 365):

Use negative indexes.

Instead of this:
last = items[len(items)-1]

Do this:
last = items[-1]

If you're moving from a language that doesn't have negative indexes, it's easy to forget about them.

That's the whole tip! It's a short one today.

#Python #DailyPythonTip
January 14, 2026 at 7:04 PM
Python Tip #13 (of 365):

Copy iterables with the list() constructor. 🧵

Need to make a new list out of an existing iterable, without changing anything?

Don't use a "for" loop, a comprehension, slicing, or the copy method.

Use list()!

#Python #DailyPythonTip
January 13, 2026 at 7:04 PM
Reposted by Trey Hunner
@anthropic.com is investing $1.5 million in the PSF, focused on security. These funds will make an enormous impact on the PSF and the security of millions of #Python and @pypi.org users. Please join us in thanking Anthropic for this landmark gift!

Read more on our blog:
Python Software Foundation News
pyfound.blogspot.com
January 13, 2026 at 1:01 PM
So when Python opens files, it normalizes all line endings to simply be represented as line feed characters (\n). Read more �‘‰ https://trey.io/ca0qyn #Python
trey.io
January 13, 2026 at 12:12 AM
Python Tip #12 (of 365):

Use "[]" to create empty lists instead of calling "list()". 🧵

(This week's tips will all be related to lists)

#Python #DailyPythonTip
January 12, 2026 at 7:04 PM
Python Tip #11 (of 365):

Avoid comparing to True and False in Python. 🧵

#Python #DailyPythonTip
January 11, 2026 at 3:18 PM
Python Tip #10 (of 365):

When checking for emptiness (or non-emptiness) in Python, use truthiness. 🧵

Instead of this:
if len(items) > 0:
...

Or this:
if len(items):
...

Do this:
if items:
...

#Python #DailyPythonTip
January 10, 2026 at 7:04 PM
I made a goal to share one #Python tip every day in 2026.

Based on the roll that Reuven's been on, I suspect he has a similar goal!

If you've been enjoying my tips, check out his as well!

We're definitely going to overlap many common tips, but there's no harm in seeing the same tip twice!
Use parentheses to split long #Python code across lines:

if x == 10 and
y == 20:
print('Yes!') # ☹️

But with parentheses, Python sees it as one line:

if (x == 10 and
y == 20): # 🙂
print('Yes!')

Or in comprehensions…

[x*5
for x in range(10)
if x % 2]
January 10, 2026 at 4:42 PM
Python Tip #9 (of 365):

When checking for None in Python, use identity instead of equality. 🧵

Use "result is None" instead of "result == None".

Using identity works because Python's None is a sentinel value, meaning there's exactly one None object in Python.

#Python #DailyPythonTip
January 9, 2026 at 4:05 PM
The hard part is figuring out which format string you need to specify to parse your date string properly. Read more �‘‰ https://trey.io/l4b84k #Python
January 9, 2026 at 1:06 AM
Python Tip #8 (of 365):

Use De Morgan's Law to make more readable Boolean expressions. 🧵

De Morgan's Law states that:

- "not (A or B)" is the same as "(not A) and (not B)"
- "not (A and B)" is the same as "(not A) or (not B)"

#Python #DailyPythonTip
January 8, 2026 at 3:04 PM
Speaking of the json module's command-line interface, you can now run it with python-m json instead of python-m json.tool Read more �‘‰ https://trey.io/flfwr9 #Python
trey.io
January 7, 2026 at 7:19 PM
Python Tip #7 (of 365):

Avoid using "or" short-circuiting for fallback reassignments. 🧵

Instead of this:

name = name or "world"

I recommend this:

if not name:
name = "world"

That second approach is more verbose, but I also find it more readable at a glance.

#Python #DailyPythonTip
January 7, 2026 at 7:04 PM
Python Tip #6 (of 365):

Use short-circuiting to collapse nested "if" statements. 🧵

These nested "if" statements:

if result:
if result.success:
print("success!")

Can instead be written with a single "if":

if result and result.success:
print("success!")

#Python #DailyPythonTip
January 6, 2026 at 5:21 PM
I'm surprised that golf courses use almost as much water as livestock. 😦
On AI & water, it looks like all US data center usage (not just AI) ranges from 628M gallons a day (counting evaporation from dam reservoirs used for hydro-power) to 200-275M with power but not dam evaporation, to 50M for cooling datacenters alone.

So not nothing, but also a lot less than golf.
January 6, 2026 at 1:30 AM