Hi Reader, There's a gift for you somewhere in this email... just look for the 🎁 emoji! 👉 Tip #39: Six quick Python tricksHere's what I'll cover below:
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 length: A set is a collection of values (like a list), except it can't contain any duplicate values: You can use this trick with any iterable, including strings: 2️⃣ Count values with Counter Need to know how many times each letter appears? Use the Counter class: Counter objects act like dictionaries, which means that if we want to know how many times 'a' appears, we pass it the 'a' key: Counters have useful methods like most_common, which returns the specified number of most common values: 3️⃣ Better debugging with f-strings You've probably used f-strings for substitution: But f-strings can also help with debugging! Normally you might print out variables within a function to check that it's working as expected: But with self-documenting expressions (new in Python 3.8), the output is more useful: The secret is to end each replacement field with an equals sign! 4️⃣ Return multiple values from a function Let's say that your function needs to return multiple values: The function returns a tuple: But if you want those return values as separate objects, you can use multiple assignment (also known as tuple unpacking): 5️⃣ Count while looping Let's say that you need to loop through a list: Sometimes you need to keep track of the index while looping. Don't do this: Use enumerate instead, which keeps track of the index for you: It actually generates a tuple, which we're unpacking into i and word! 6️⃣ Create a dictionary with a comprehension List comprehensions are useful all over the place in Python: By slightly tweaking the syntax, you can write a dictionary comprehension instead: 📈 Going furtherMany of these tricks are taken from my online course, Python Essentials for Data Scientists:
🎁 Just for fun, I'm giving readers of Tuesday Tips a 25% discount on this course for the next week. Use code TUESDAY25 at checkout! 🎁 👋 Until next timeDid you like this week’s tip? Please send it to a friend or share this link on social. It really helps me out! 🙌 See you next Tuesday! - Kevin P.S. Uber's new "Save with Ads" option (parody) 😂 Did someone AWESOME forward you this email? Sign up here to receive Data Science tips every week! |
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...