profile

Learn Data Science from Data School 📊

Tuesday Tip #20: Dictionary comprehensions in Python 📚

Published 8 months ago • 1 min read

Hi Reader,

Tomorrow, I’m re-launching Python Essentials for Data Scientists to celebrate a HUGE course upgrade:

  • NEW: 22 additional video lessons
  • NEW: 7-part project to practice everything you’re learning

There will be a limited-time offer to celebrate the re-launch, so watch out for tomorrow’s email! 💸

I’ll be sending a few extra emails this week. I know your inbox is precious, so I’ll be giving you FREE access to 3 modules from the course! That way, you can benefit regardless of whether you choose to enroll. 💌

Thank YOU for being a valued reader! 🙏 Now let’s get to today’s tip…


👉 Tip #20: Use dictionary comprehensions in Python

Let’s say we had this list of words:

If we wanted to create a list of the word lengths, we could use a for loop:

We start with an empty list, and each time the loop runs, len(word) is appended to the list.

But as you might know, a list comprehension is even better for this scenario:

The effect is the same, but the code is much more concise.

Let’s now pretend that we wanted to create a dictionary containing both the word and its length. Once again, let’s use a for loop:

This time, we start with an empty dictionary, and each time the loop runs, a key-value pair is added to the dictionary.

You can actually convert this into a dictionary comprehension:

The structure is similar to a list comprehension, except there are curly braces { } instead of brackets [ ], and the first part of the comprehension (called the “expression”) is word:len(word) instead of just len(word).

You can read it as follows: “For each word, create a key-value pair of the word and its length.”

Visually, I really like the dictionary comprehension, since the “key:value” structure of the expression matches the way the dictionary prints out!

Let me know if you have any questions! 💬


Today’s tip is just one of the 22 NEW lessons from Python Essentials for Data Scientists!

If you want to build a solid foundation in Python and stay relevant in the world of AI, watch out for tomorrow’s email!

- Kevin

P.S. Weird Al Yankovic is human!

Do you know someone who could benefit from learning Python? Please send them to pythoncourse.io so that they can hear about the launch! 🚀

Learn Data Science from Data School 📊

Kevin Markham

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, Do any of these sound like you? You're new to the pandas library and you want to learn the fundamentals You have some experience with pandas, but you want to fill in the gaps in your knowledge You want to learn the best practices for data analysis with pandas in 2024 If so, you should enroll in my FREE course (launching today!), pandas in 30 days. Why learn pandas? pandas is a powerful, open source Python library for data analysis, manipulation, and visualization. If you're working...

9 days ago • 1 min read

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: Return the number of unique values Count values with Counter Better debugging with f-strings Return multiple values from a function Count while looping 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...

16 days ago • 2 min read

Hi Reader, My goal with Tuesday Tips is to help you get better at Data Science every week. Is there anything that would make these tips even more helpful for you? Let me know! 💬 You can find past tips at tuesday.tips. (Yes, that’s a real URL!) Tip #38: Five ways to rename your DataFrame columns Let's say that we have a simple pandas DataFrame: I prefer to use dot notation to select pandas columns, but that won't work since the column names have spaces. Let's fix this! The most flexible method...

23 days ago • 1 min read
Share this post