Data Structures

Python Join List with Underscore [The Most Pythonic Way]

The ‘_’.join(list) method on the underscore string ‘_’ glues together all strings in the list using the underscore string as a delimiter—and returns a new string. For example, the expression ‘_’.join([‘a’, ‘b’, ‘c’]) returns the new string ‘a_b_c’. Definition and Usage The string.join(iterable) method joins the string elements in the iterable to a new string …

Python Join List with Underscore [The Most Pythonic Way] Read More »

[NumPy vs Python] What are Advantages of NumPy Arrays over Regular Python Lists?

The Python built-in list data type is powerful. However, the NumPy array has many advantages over Python lists. What are they? Advantages NumPy Advantages Python Lists Multi-dimensional Slicing Library-Independent Broadcasting Functionality Intuitive Processing Speed Less Complicated Memory Footprint Heterogeneous List Data Allowed Many Convenience Methods Arbitrary Data Shape (Non-Square Matrix) Let’s dive into the most …

[NumPy vs Python] What are Advantages of NumPy Arrays over Regular Python Lists? Read More »

Python List to Set Conversion [Interactive Guide]

Do you have a list but you want to convert it to a Python set? No problem! Use the set(…) constructor and pass the list object as an argument. For example, if you have a list of strings friends, you can convert it to a set using the call set(friends). Here’s an example code snippet: …

Python List to Set Conversion [Interactive Guide] Read More »

How to Filter a Dictionary in Python? (… The Most Pythonic Way)

Problem: Given a dictionary and a filter condition. How to filter a dictionary by … … key so that only those (key, value) pairs in the dictionary remain where the key satisfies the condition? … value so that only those (key, value) pairs remain where the value satisfies the condition? In this tutorial, you’ll learn …

How to Filter a Dictionary in Python? (… The Most Pythonic Way) Read More »

Complexity of Python Operations

In this tutorial, you’ll learn the runtime complexity of different Python operations. Then, you’ll learn how to calculate the complexity of your own function by combining the complexity classes of its constituents. This is called “static analysis” The tutorial is loosely based on (source) but it extends it significantly with more practical examples, interactive snippets, …

Complexity of Python Operations Read More »

How to Add an Element to a Python List at an Index?

To add an element to a given Python list, you can use either of the three following methods: Use the list insert method list.insert(index, element). Use slice assignment lst[index:index] = [element] to overwrite the empty slice with a list of one element. Use list concatenation with slicing lst[:2] + [‘Alice’] + lst[2:] to create a …

How to Add an Element to a Python List at an Index? Read More »

How to Find the Minimum of a List of Lists in Python?

Short answer: To find the minimal list in a list of lists, you need to make two lists comparable. How? With the key argument of the min() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the smallest numerical value is …

How to Find the Minimum of a List of Lists in Python? Read More »

How to Find the Max of a List of Lists in Python?

Short answer: To find the maximal list in a list of lists, you need to make two lists comparable. How? With the key argument of the max() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the largest numerical value is …

How to Find the Max of a List of Lists in Python? Read More »