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 Artificial Intelligence from Data School 🤖

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School 🤖

Hi Reader, Last week, I encouraged you to experiment with different LLMs, since there’s no one model that is superior across all use cases. Specifically, I suggested you try using Chatbot Arena, which allows you to chat with multiple models at once. It’s completely free, but has two significant disadvantages: Your chats are not private and may be used for research. It lacks the feature-rich interface provided by other LLMs. Today, I want to offer you a better method for experimenting with...

Hi Reader, Over the past 50 tips, I’ve touched on many different topics: Python, Jupyter, pandas, ML, data visualization, and so on. Going forward, I’m planning to focus mostly on Artificial Intelligence. I’m announcing this so you know what to expect, and I know what to deliver! 💌 I’ll also try to make the tips shorter, so that they're easier to digest on-the-go. Finally, I plan to include an “action item” each week, so that you can practice what you’re learning. I hope you like these...

Hi Reader, Next week, I’ll be offering a Black Friday sale on ALL of my courses. I’ll send you the details this Thursday! 🚨 👉 Tip #50: What is a "method" in Python? In Python, a method is a function that can be used on an object because of the object's type. For example, if you create a Python list, the "append" method can be used on that list. All lists have an "append" method simply because they are lists: If you create a Python string, the "upper" method can be used on that string simply...