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, Until 8 PM ET tonight, you can get the All-Access Pass for $99: Here's everything you need to know: Access all existing courses for one year ($700+ value) Includes new courses launched during your subscription Includes e-book version of Master Machine Learning (coming soon) Additional discounts available Lock in this price forever 30-day refund policy Get the Pass for $99 Questions? Please let me know! - Kevin
Hi Reader, I wanted to share with you three limited-time resources for improving your Python skills... 1️⃣ Algorithm Mastery Bootcamp 🥾 Are you looking for an intense, 12-day Python bootcamp? My friend Rodrigo Girão Serrão is running a new Algorithm Mastery Bootcamp, and it starts in just 5 days! In the bootcamp, you'll solve 24 real programming challenges and participate in daily live sessions to discuss and compare solutions. It's a great way to strengthen your problem-solving muscles 💪 I...
Hi Reader, Last week, I launched the All-Access Pass, which gives you access to ALL of Data School's courses for one year. Through Black Friday, you can buy the pass for $99, after which the price will increase. Here are the included courses: Build an AI chatbot with Python ($9) Create your first AI app in 60 minutes using LangChain & LangGraph! ⚡ Build AI agents with Python ($99) Develop the skills to create AI apps that can think and act independently 🤖 Conda Essentials for Data Scientists...