Print a Python List Beautifully [Click & Run Code]

How to print a Python list in a beautiful and fully customizable way? This article shows you six effective ways of doing it. By studying these alternatives, you’ll not only learn how to print lists in Python, you’ll become a better coder overall. If you just want to know the best way to print a … Read more

Matplotlib 3D Plot [Tutorial]

Photo by Aida L on Unsplash

Are you tired with the same old 2D plots? Do you want to take your plots to the next level? Well look no further, it’s time to learn how to make 3D plots in matplotlib. In addition to import matplotlib.pyplot as plt and calling plt.show(), to create a 3D plot in matplotlib, you need to: … Read more

How to Get the Number of Elements in a Python List?

To get the number of elements in a Python list, use the len(list) function. You don’t need to import any external library because len() is a Python built-in function. You can use it to get the number of elements of several built-in and library types in Python such as tuples and sets. Here’s the minimal … Read more

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