Data Structures

Creating, Reading & Updating A Config File with Python

Subtitle: Avoid Hard-Coded Data While Making Your (And Others) Life Easier When the subject of config, or configuration, files comes up there is often confusion on what they are, how to read them, and the benefits that come from their use. In this article, we will cover what config files are before introducing the Python …

Creating, Reading & Updating A Config File with Python Read More »

Python Set copy()

Python’s set.copy() method creates and returns a flat copy of this set. Here’s a minimal example where you copy a set with two integers and a string value: Syntax Let’s dive into the formal syntax of the set.copy() method. set.copy() Argument Data Type Explanation – – – The set.copy() method doesn’t take any argument. If …

Python Set copy() Read More »

Python Set add()

Python’s set.add(element) method adds the element to this set. If the element is already present in the set, the method returns without any side effect. Here’s a minimal example where we add element 4 to an existing set: Syntax Let’s dive into the formal syntax of the set.add() method. set.add(element) Argument Data Type Explanation element …

Python Set add() Read More »

How to Dynamically Create a Function in Python?

Problem Formulation There are different variants of this problem that all ask the same thing: How to create a function dynamically in Python? How to define a function at runtime? How to define a function programmatically? How to create a function from a string? There are many ways to answer these questions—most web resources provide …

How to Dynamically Create a Function in Python? Read More »

How to Convert a String List to an Integer List in Python

The most Pythonic way to convert a list of strings to a list of ints is to use the list comprehension [int(x) for x in strings]. It iterates over all elements in the list and converts each list element x to an integer value using the int(x) built-in function. This article shows you the simplest …

How to Convert a String List to an Integer List in Python Read More »

How to Convert a String List to a Float List in Python

The most Pythonic way to convert a list of strings to a list of floats is to use the list comprehension floats = [float(x) for x in strings]. It iterates over all elements in the list and converts each list element x to a float value using the float(x) built-in function. This article shows you …

How to Convert a String List to a Float List in Python Read More »

How to Convert an Integer List to a Float List in Python

The most Pythonic way to convert a list of integers ints to a list of floats is to use the list comprehension expression floats = [float(x) for x in ints]. It iterates over all elements in the list ints using list comprehension and converts each list element x to a float value using the float(x) …

How to Convert an Integer List to a Float List in Python Read More »

How to Convert a Float List to a String List in Python

The most Pythonic way to convert a list of floats fs to a list of strings is to use the one-liner fs = [str(x) for x in fs]. It iterates over all elements in the list fs using list comprehension and converts each list element x to a string value using the str(x) constructor. This …

How to Convert a Float List to a String List in Python Read More »

How to Convert an Integer List to a String List in Python

The most Pythonic way to convert a list of integers ints to a list of strings is to use the one-liner strings = [str(x) for x in ints]. It iterates over all elements in the list ints using list comprehension and converts each list element x to a string value using the str(x) constructor. This …

How to Convert an Integer List to a String List in Python Read More »