Python 3.8 Walrus Operator (Assignment Expression)

The release of Python 3.8 came with an exciting new feature: the Walrus Operator. It’s an assignment expression, or in simpler terms an assignment inside an expression. Let’s dive into practice immediately and look at the following code: We have a list of users whose data is represented in a dictionary. We want to count … Read more

Python List Methods Cheat Sheet [Instant PDF Download]

Here’s your free PDF cheat sheet showing you all Python list methods on one simple page. Click the image to download the high-resolution PDF file, print it, and post it to your office wall: I’ll lead you through all Python list methods in this short video tutorial: If you want to study the methods yourself, … Read more

List to Dict — Convert a List Into a Dictionary in Python

That’s a very common question and the answer is: It depends.It depends on the data in the list and how you want it to be represented in the dictionary. In this article we go over six different ways of converting a list to a dictionary. You can also watch my advanced one-liner video where we … Read more

How to Convert Two Lists Into A Dictionary

Want to convert two lists into a dictionary? Do the following: Zip the lists together using zip(list_1, list_2). Create a dictionary from the list of tuples using the constructor dict() on the result. In other words, call dict(zip(list_1, list_2)) to convert two lists into a dictionary. Try it yourself: Let’s dive into the code step-by-step. … Read more

How to Calculate the Column Standard Deviation of a DataFrame in Python Pandas?

Want to calculate the standard deviation of a column in your Pandas DataFrame? In case you’ve attended your last statistics course a few years ago, let’s quickly recap the definition of variance: it’s the average squared deviation of the list elements from the average value. You can do this by using the pd.std() function that … Read more

How to Use Generator Expressions in Python Dictionaries

Imagine the following scenario: You work in law enforcement for the US Department of Labor, finding companies that pay below minimum wage so you can initiate further investigations. Like hungry dogs on the back of a meat truck, your Fair Labor Standards Act (FLSA) officers are already waiting for the list of companies that violated … Read more

Python List Average

Don’t Be Mean, Be Median. This article shows you how to calculate the average of a given list of numerical inputs in Python. In case you’ve attended your last statistics course a few years ago, let’s quickly recap the definition of the average: sum over all values and divide them by the number of values. … Read more

100 Code Puzzles to Train Your Rapid Python Understanding

Some coders possess laser-like code understanding skills. They look at a piece of source code, and the meaning pops immediately into their great minds. If you present them with a new code snippet, they’ll tell you in a few seconds what it does. Since I’ve started to train this skill of rapid code understanding in … Read more