Tuesday Tip #20: Dictionary comprehensions in Python 📚


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 📊

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:...