Python Lists filter() vs List Comprehension – Which is Faster?

[Spoiler] Which function filters a list faster: filter() vs list comprehension? For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method. To answer this question, I’ve written a short script that tests the runtime performance of filtering large lists of increasing sizes using the filter() … Read more

How Does Nested List Comprehension Work in Python?

Nested List Comprehension—what does it even mean? There are three equally interpretations of this term: Coming from a computer science background, I was assuming that “nested list comprehension” refers to the creation of a list of lists. In other words: How to create a nested list with list comprehension? But after a bit of research, … Read more

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