How to Remove Duplicates From a Python List?

Do you know the best way of removing duplicates from a Python list? This is a popular coding interview question at Google, Facebook, and Amazon. In this article, I’ll show you how (and why) it works—so keep reading! How to remove all duplicates of a given value in the list? Naive Method: Go over each … Read more

[Resolve] IndexError: List Assignment Index Out of Range

When does the IndexError: list assignment index out of range appear? Python throws an IndexError if you try to assign a value to a list index that doesn’t exist, yet. For example, if you execute the expression list[1] = 10 on an empty list, Python throws the IndexError. Simply resolve it by adding elements to … Read more

Python List to String: A Helpful Guide with Interactive Shell

Problem: Given a list of strings. How to convert the list to a string by concatenating all strings in the list? Example: You want to convert list [‘learn ‘, ‘python ‘, ‘fast’] to the string ‘learn python fast’. Solution: to convert a list of strings to a string, do the following. Call the ”.join(list) method … Read more

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