The World’s Most Concise Python Cheat Sheet

Do you want to learn Python but you’re overwhelmed and you don’t know where to start? Learn with Python cheat sheets! They compress the most important information in an easy-to-digest 1-page format. Here’s the new Python cheat sheet I just created—my goal was to make it the world’s most concise Python cheat sheet! ChrisWhile working … Read more

The Most Pythonic Way to Compare Two Lists in Python

Problem: Given are two lists l1 and l2. You want to perform either of the following: 1. Boolean Comparison: Compare the lists element-wise and return True if your comparison metric returns True for all pairs of elements, and otherwise False. 2. Difference: Find the difference of elements in the first list but not in the … Read more

The Most Pythonic Way to Check if Two Unordered Lists Are Identical

To check if two unordered lists x and y are identical, compare the converted sets with set(x) == set(y). However, this loses all information about duplicated elements. To consider duplicates, compare the sorted lists with sorted(x) == sorted(y). Due to the efficient merge-sort-like implementation of the sorted() function, this is quite fast for almost-sorted lists. … Read more

How to Use Python’s Join() on a List of Objects (Not Strings)?

The most Pythonic way to concatenate a list of objects is the expression ”.join(str(x) for x in lst) that converts each object to a string using the built-in str(…) function in a generator expression. You can concatenate the resulting list of strings using the join() method on the empty string as a delimiter. The result … Read more

How to Join Specific List Elements in Python?

To join specific list elements (e.g., with indices 0, 2, and 4) and return the joined string that’s the concatenation of all those, use the expression ”.join([lst[i] for i in [0, 2, 4]]). The list comprehension statement creates a list consisting of elements lst[0], lst[2], and lst[4]. The ”.join() method concatenates those elements using the … Read more

The Most Pythonic Way to Join a List in Reverse Order

The most efficient method to join a list of Python strings in reverse order is to use the Python code ”.join(l[::-1]). First, reverse the list l using slicing with a negative step size l[::-1]. Second, glue together the strings in the list using the join(…) method on the empty string ”. Problem: Given a list … 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, … Read more