## Python – Return NumPy Array From Function

Do you need to create a function that returns a NumPy array but you don’t know how? No worries, in sixty seconds, you’ll know! Go! π A Python function can return any object such as a NumPy Array. To return an array, first create the array object within the function body, assign it to a … Read more

## How to Return an Empty List from a Function in Python?

Method 1: Return a New Empty List Using Square Brackets You can return an empty new list from a Python function using the square bracket notation [] after the return keyword using the expression return []. Here’s an example: The example also shows that the two created empty lists using two function calls point to … Read more

## Python Return Boolean (True/False) From Function

Do you need to create a function that returns a Boolean (True or False) but you don’t know how? No worries, in sixty seconds, you’ll know! Go! π₯π₯π₯ A Python function can return any object such as a Boolean value (True or False). To return a Boolean, you can have an arbitrary simple or complex … Read more

## Python Return String From Function

Do you need to create a function that returns a string but you don’t know how? No worries, in sixty seconds, you’ll know! Go! π A Python function can return any object such as a string. To return a string, create the string object within the function body, assign it to a variable my_string, and … Read more

## Python – List of Lists to List of Strings. How?

Coding Challenge π¬ Coding Challenge: Given a nested Python list, i.e., a list of lists. How to convert it to a string list, so that each inner list becomes a single string? Consider the following examples: 1. Input: [[‘h’, ‘e’, ‘l’, ‘l’, ‘o’], [‘w’, ‘o’, ‘r’, ‘l’, ‘d’]] Output: [‘hello’, ‘world’] 2. Input: [[‘h’, ‘i’], … Read more

## Python Function Return None Without Return Statement

The Python return keyword allows you to define the return value of a function (e.g., return X). If you don’t explicitly set a return value or you omit the return statement, Python will implicitly return the following default value: None. There are three cases of what a function can return: Case 1: The function has … Read more

## How to Apply a Function to a Python List

Problem Formulation and Solution Overview This article will show you how to apply a function to a List in Python. To make it more interesting, we have the following running scenario: As a Python assignment, you have been given a List of Integers and asked to apply a function to each List element in various … Read more

## How to Apply a Function to Each Cell in a Pandas DataFrame?

Problem Formulation Given the following DataFrame df: π¬ Challenge: How to apply a function f to each cell in the DataFrame? For example, you may want to apply a function that replaces all odd values with the value ‘odd’. Solution: DataFrame applymap() The Pandas DataFrame df.applymap() method returns a new DataFrame where the function f … Read more

## pd.agg() – Aggregating Data in Pandas

The name agg is short for aggregate. To aggregate is to summarize many observations into a single value that represents a certain aspect of the observed data. The .agg() function can process a dataframe, a series, or a grouped dataframe. It can execute many aggregation functions, e.g. βmeanβ, βmaxβ,β¦ in a single call along one … Read more

## How to Apply a Function to Column Elements

Problem Formulation and Solution Overview As a Python Coder, situations arise where you will need to apply a function against elements of a DataFrame Column. To make it more fun, we have the following running scenario: You have a DataFrame containing user information (including the column Recurring). This column is the Monthly Fee for a … Read more

## How to Limit the Execution Time of a Function Call?

Problem Formulation Say you need to call a potentially long function but you can only wait for a fixed time duration for the function to terminate. If the function does terminate within the time interval, you take the function’s return value. If the function does not terminate within the time interval, you want to take … Read more

## Python Slice Get First and Last Element

Problem Formulation Given a Python iterable such as a list or a string that can be accessed via slicing. How to use the slicing operation iter[start, stop, step] so that for given values for start, stop, and step the output is an iterable (slice) containing only the first and the last element of the iterable? … Read more