Trey Hunner 🐍

@treyhunner
2.6K Followers
325 Following
4.8K Posts

#Python & #Django educator & team trainer

I help folks sharpen their Python skills with https://PythonMorsels.com🐍πŸͺ

#pythonoddity

Also: ostrovegan, sentientist, YIMBY, and I think economics is highly underrated. I don't post about any of those topics very often.

he/him

πŸ’Œ My Weekly Newsletterhttps://pym.dev/newsletter
🐍 Python Exerciseshttps://www.pythonmorsels.com
πŸ“Ί YouTubehttps://www.youtube.com/@PythonMorsels
πŸ•Έ Personal Bloghttps://treyhunner.com

β€’ "a | b" gives the UNION
β€’ "a & b" gives the INTERSECTION
β€’ "a - b" gives the DIFFERENCE (in a, but not b)
β€’ "a ^ b" gives the SYMMETRIC DIFFERENCE (items in either set, but not both)

When you want to compare the items in two collections, consider whether set arithmetic might help.

For example, to find which required fields are missing:

required = {"name", "email", "password"}
provided = set(form_data.keys())
missing = required - provided

More: https://pym.dev/practical-uses-of-sets/

🧡 (2/2)

Practical uses of sets

Sets are unordered collections of values that are great for removing duplicates, quick containment checks, and set operations.

Python Tip #164 (of 365):

Thinking in terms of a Venn diagram? You need a set.

Need to find all items in one collection but not another? Intersect two collections? Check what's unique to each?

Python's sets support set arithmetic using operators:

>>> a = {1, 2, 3, 4, 7}
>>> b = {1, 3, 5, 7, 9}
>>> a | b
{1, 2, 3, 4, 5, 7, 9}
>>> a & b
{1, 3, 7}
>>> a - b
{2, 4}
>>> a ^ b
{2, 4, 5, 9}

🧡 (1/2)

#Python #DailyPythonTip

I think I know countries and then I realize I don’t. Love this game so much!

πŸ—ΊοΈ June 13, 2026
5/5 countries in 9/13 guesses
🟒🟑🟑🟑🟑
https://whereabouts.earth/daily/
#Whereabouts

Whereabouts Daily

How many of today's five countries can you guess?

Whereabouts
Whereabouts Daily
πŸ—ΊοΈ June 13, 2026
5/5 countries in 6/13 guesses
🟒🟒🟒🟒🟑
https://whereabouts.earth/daily/
#Whereabouts
Whereabouts Daily

How many of today's five countries can you guess?

Whereabouts

Whereabouts is the first geography game that gets enough things right that I end up playing every day. I've always wanted to know the world map better, and I'm finally making steady progress. :)

Thanks @treyhunner!

πŸ—ΊοΈ June 13, 2026
4/5 countries in 13/13 guesses
🟒βšͺ🟠🟑🟑
https://whereabouts.earth/daily/
#Whereabouts

Whereabouts Daily

How many of today's five countries can you guess?

Whereabouts

If you find yourself doing containment checks inside a loop, your code has an O(n) operation for each step of another O(n) operation, making it O(n*n).

This O(n*n) code may take seconds over a large list:

for word in words:
if word[::-1] in words:
print(word)

While this O(n) code may takes milliseconds:

word_set = set(words)
for word in words:
if word[::-1] in word_set:
print(word)

For more on this topic, see my article on this: (https://pym.dev/time-complexities/

🧡 (4/4)

Python Big O: the time complexities of different data structures in Python

The time complexity of common operations on Python's many data structures.

Here are the ones worth memorizing for Python's built-in data structures:

LISTS:
β€’ Indexing (items[i]): O(1)
β€’ Append/pop from end: O(1)
β€’ Insert/pop from beginning: O(n)
β€’ Containment check (x in items): O(n)
β€’ Sorting: O(n log n)

SETS AND DICTIONARIES:
β€’ Containment check (x in items): O(1)
β€’ Add/remove: O(1)

🧡 (3/4)

Here are the most common time complexities you'll see in Python, from fastest to slowest:

β€’ O(1) (constant): the work doesn't grow with the data, e.g. indexing a list

β€’ O(N) (linear): the work grows with the data, e.g. looping over a list

β€’ O(N LOG N): a bit slower than linear, e.g. sorting a list

β€’ O(n*n) (quadratic): the work grows with the square of the data, e.g. a loop within a loop

🧡 (2/4)

Python Tip #163 (of 365):

Commit the most important time complexities to memory.

Time complexity is about how your code slows as your data grows (I stole that phrase from Ned Batchelder).

You don't need a Computer Science degree to benefit from knowing a handful of common complexities.

🧡 (1/4)

#Python #DailyPythonTip

β€œUnlike many programming languages, you can accomplish quite a bit in Python without ever making a class.”

Read more πŸ‘‰ https://pym.dev/when-are-classes-used/

#Python #classes

When are classes used in Python?

While you don't often need to make your own classes in Python, they can sometimes make your code reusable and easier to read.