Tuesday Tip #39: Six quick Python tricks 🎯


Hi Reader,

There's a gift for you somewhere in this email... just look for the 🎁 emoji!


👉 Tip #39: Six quick Python tricks

Here's what I'll cover below:

  1. Return the number of unique values
  2. Count values with Counter
  3. Better debugging with f-strings
  4. Return multiple values from a function
  5. Count while looping
  6. Create a dictionary with a comprehension

Let's get started! 👇


1️⃣ Return the number of unique values

Need to know the number of unique values in an iterable? Convert it to a set and check the length:

A set is a collection of values (like a list), except it can't contain any duplicate values:

You can use this trick with any iterable, including strings:


2️⃣ Count values with Counter

Need to know how many times each letter appears? Use the Counter class:

Counter objects act like dictionaries, which means that if we want to know how many times 'a' appears, we pass it the 'a' key:

Counters have useful methods like most_common, which returns the specified number of most common values:


3️⃣ Better debugging with f-strings

You've probably used f-strings for substitution:

But f-strings can also help with debugging!

Normally you might print out variables within a function to check that it's working as expected:

But with self-documenting expressions (new in Python 3.8), the output is more useful:

The secret is to end each replacement field with an equals sign!


4️⃣ Return multiple values from a function

Let's say that your function needs to return multiple values:

The function returns a tuple:

But if you want those return values as separate objects, you can use multiple assignment (also known as tuple unpacking):


5️⃣ Count while looping

Let's say that you need to loop through a list:

Sometimes you need to keep track of the index while looping. Don't do this:

Use enumerate instead, which keeps track of the index for you:

It actually generates a tuple, which we're unpacking into i and word!


6️⃣ Create a dictionary with a comprehension

List comprehensions are useful all over the place in Python:

By slightly tweaking the syntax, you can write a dictionary comprehension instead:


📈 Going further

Many of these tricks are taken from my online course, Python Essentials for Data Scientists:

  • The first half of the course will help you build a solid foundation in Python's data structures, operators, functions, control flow, and best practices.
  • The second half of the course will challenge you with a 7-part project, in which you'll practice everything you've learned plus dive deeper into intermediate Python topics.

🎁 Just for fun, I'm giving readers of Tuesday Tips a 25% discount on this course for the next week. Use code TUESDAY25 at checkout! 🎁


👋 Until next time

Did you like this week’s tip? Please send it to a friend or share this link on social. It really helps me out! 🙌

See you next Tuesday!

- Kevin

P.S. Uber's new "Save with Ads" option (parody) 😂

Did someone AWESOME forward you this email? Sign up here to receive Data Science tips every week!

Learn Data Science from Data School 📊

Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!

Read more from Learn Data Science from Data School 📊

Hi Reader, Regardless of whether you enrolled, thanks for sticking with me through the launch of my new course! 🚀 I've already started exploring topics for the next course... 😄 🔗 Link of the week git cheat sheet (PDF) A well-organized and highly readable cheat sheet from Julia Evans, the brilliant mind behind Wizard Zines! 👉 Tip #48: Three ways to set your environment variables in Python I was playing around with Mistral LLM this weekend (via LangChain in Python), and I needed to set an...

Hi Reader, Last week, I announced that a new course is coming soon and invited you to guess the topic. Hundreds of guesses were submitted, and four people who guessed correctly got the course for free! (I've already notified the winners.) I'll tell you about the course next week. In the meantime, I've got a new Tuesday Tip for you! 👇 🔗 Link of the week OpenAI just unleashed an alien of extraordinary ability (Understanding AI) If you're curious about what makes OpenAI's new "o1" models so...

Hi Reader, I'm really proud of this week's tip because it covers a topic (data leakage) that took me years to fully understand. 🧠 It's one of those times when I feel like I'm truly contributing to the collective wisdom by distilling complex ideas into an approachable format. 💡 You can read the tip below 👇 or on my blog. 🔗 Link of the week Building an AI Coach to Help Tame My Monkey Mind (Eugene Yan) In this short post, Eugene describes his experiences calling an LLM on the phone for coaching:...