Scripting

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 »

How to Get an HTML Page from a URL in Python?

This tutorial shows you how to perform simple HTTP get requests to get an HTML page from a given URL in Python! Problem Formulation Given a URL as a string. How to extract the HTML from the given URL and store the result in a Python string variable? Example: Say, you want to accomplish the …

How to Get an HTML Page from a URL in Python? Read More »

Coding Your Own Google Home and Launch Spotify in Python

Doesn’t this project sound exciting? Project goal: code your own Google Home with Python and learn how to use speech recognition to launch Spotify and play songs! Ever wanted to code a powerful yet simple tool that is more bespoke than mainstream devices? We will learn how to implement it in Python with a bunch …

Coding Your Own Google Home and Launch Spotify in Python Read More »

How to Loop Through a Python List in Pairs, Sliding Windows, and Batches?

Method 1: Iterating over Consecutive (Sliding) Windows Given are: Python list lst Window size n Problem Formulation: How to loop through the list in consecutive element-windows of size n, so that in each iteration, you can access the n next elements in the list? # INPUT: lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’] …

How to Loop Through a Python List in Pairs, Sliding Windows, and Batches? Read More »

Python iter() — A Simple Illustrated Guide with Video

Python’s built-in iter() function returns an iterator for the given object. For example, iter([1, 2, 3]) creates an iterator for the list [1, 2, 3]. You can then iterate over all elements in the iterator, one element at a time, in a for or while loop such as: for x in iter([1, 2, 3]). Basic …

Python iter() — A Simple Illustrated Guide with Video Read More »