Hi Reader,
Tomorrow, I’m re-launching Python Essentials for Data Scientists to celebrate a HUGE course upgrade:
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…
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! 🚀
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, Here are your top AI stories for the week: ChatGPT can weaken your brain Claude shares nerve gas recipe Amsterdam ends AI experiment due to bias Read more below! 👇 Sponsored by: Brain.fm Transform Your Focus With Brain.fm I know you're always on the hunt for tools that genuinely improve your life—which is why I'm excited to introduce you to Brain.fm's groundbreaking focus music. Brain.fm's patented audio technology was recently validated in a top neuroscience journal, showing how...
Hi Reader, Last week, I invited you to help me test Google's Data Science Agent in Colab, which promises to automate your data analysis. Does it live up to that promise? Let's find out! 👇 Sponsored by: Morning Brew Business news you’ll actually enjoy Join 4M+ professionals who start their day with Morning Brew—a free daily newsletter that makes business, tech, and finance news genuinely enjoyable to read and hard to forget. Each morning, it breaks down complex stories in plain English—cutting...
Hi Reader, Today I'm trying something brand new! I wrote short summaries of the 5 most important AI stories this week, and also turned it into a video: Watch the video I'd love to know what you think! đź’¬ AI-generated TV ad airs during NBA finals Prediction market Kalshi just aired this AI-generated ad on network TV during the NBA finals. It was created in just two days by one person using Google's new Veo 3 video model, plus scripting help from Google's Gemini chatbot. Expect to see many more...