Python One Line Quicksort

In this one-liner tutorial, you’ll learn about the popular sorting algorithm Quicksort. Surprisingly, a single line of Python code is all you need to write the Quicksort algorithm! Problem: Given a list of numerical values (integer or float). Sort the list in a single line of Python code using the popular Quicksort algorithm! Example: You … 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

List Difference | The Most Pythonic Way

Short answer: The most Pythonic way to compute the difference between two lists l1 and l2 is the list comprehension statement [x for x in l1 if x not in set(l2)]. This works even if you have duplicate list entries, it maintains the original list ordering, and it’s efficient due to the constant runtime complexity … 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

The Most Pythonic Way to Check if a Python String Contains Another String? (Tutorial + Video)

How to check if a Python string s1 contains another string s2? There are two easy ways to check whether string s1 contains string s2: You can try both methods in our interactive Python shell (just click “Run” to execute the code in your browser): Puzzle: Can you modify “Method 2” so that it also … Read more