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

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

Lambda Functions in Python: A Simple Introduction

A lambda function is an anonymous function in Python. It starts with the keyword lambda, followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. For example, lambda x, y, z: x+y+z would calculate the sum of the three argument values x+y+z. Here’s a practical example where … Read more

Python List max()

Do you want to find the maximum of a Python list? This article gives you everything you need to know to master the max() function in Python. Description Python’s built-in max() function returns the maximum element of a list or its generalization (an iterable). Syntax The syntax of the max() function is as follows: Arguments … Read more

Dict to List — How to Convert a Dictionary to a List in Python

Summary: To convert a dictionary to a list of tuples, use the dict.items() method to obtain an iterable of (key, value) pairs and convert it to a list using the list(…) constructor: list(dict.items()). To modify each key value pair before storing it in the list, you can use the list comprehension statement [(k’, v’) for … Read more