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

Python One Line X

This is a running document in which I’ll answer all questions regarding the single line of Python code. If you want to become a one-liner wizard, check out my book “Python One-Liners”! πŸ™‚ This document contains many interactive code shells and videos to help you with your understanding. However, it’s pretty slow because of all … Read more

How to Create a List of Dictionaries in Python?

Problem: Say, you have a dictionary {0: ‘Alice’, 1: ‘Bob’} and you want to create a list of dictionaries with copies of the original dictionary: [{0: ‘Alice’, 1: ‘Bob’}, {0: ‘Alice’, 1: ‘Bob’}, {0: ‘Alice’, 1: ‘Bob’}]. You use list comprehension with a “throw-away” loop variable underscore _ to create a list of 3 elements. … Read more

Zip With List Output Instead of Tuple | Most Pythonic Way

Short answer: Per default, the zip() function returns a zip object of tuples. To obtain a list of lists as an output, use the list comprehension statement [list(x) for x in zip(l1, l2)] that converts each tuple to a list and stores the converted lists in a new nested list object. Intermediate Python coders know … Read more

How to Use np.linspace() in Python? A Helpful Illustrated Guide

In this article, I’ll explain the np.linspace function, how to use it and when you should. It has got a bit of a reputation for being complicated but, as you’ll see, it really isn’t! So, let’s get a quick overview first. Syntax: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0) Argument Default Description start – The … Read more

Create a List of Random Numbers — The Most Pythonic Way

Do you want to initialize a list with some random numbers? In this article, I’ll show you four different way of accomplishing this—along with a short discussion about “the most Pythonic way”. Problem: Given an integer n. Create a list of n elements in a certain interval (example interval: [0, 20]). Solution: Here’s a quick … Read more

Python SymPy — A Short Primer

What’s SymPy Anyway? SymPy is a Python library for symbolic computation. So instead of approximating the result of the square root of 2, it keeps the square root intact—using a symbolic representation. This helps in further processing and can lead to situations where Python has introduced a floating point precision error without need. Here’s a … Read more

How do you install SymPy in Python?

To install SymPy in Python, simply run the following command in your shell: This works for all major operating systems (MacOS, Windows, Linux). A preliminary is to have the pip package manager installed. (Confused by all the libraries, modules, pip, and virtual environments? Read the ultimate library guide on my blog.) To check whether it … Read more

Python String to List | The Most Pythonic Way

Programming is a humbling experience. Seemingly simple things will often surprise you, they’re not so trivial after all. One such example is the string to list conversion. In this article, you’ll learn everything you need to know to convert a string to a list—in different contexts and using different methods. Method Description list(string) Break a … Read more