Daily Python Puzzle

How to Find the Maximum Value in a Python Dict?

There are three problem variants of finding the maximum value in a Python dictionary: Find the maximum value and return this maximum value Find the maximum value and return a (key, value) tuple of both the key and the max value itself Find the maximum value and return only the key assigned to the max …

How to Find the Maximum Value in a Python Dict? Read More »

How to Sort in Python with the Lambda Function as Key Argument?

Challenge: This article shows how to sort a Python list using the key argument that takes a function object as an argument. Quick Solution: You can use the lambda function to create the function on the fly as shown in the following example: Explanation: The code uses the key argument and passes a lambda function …

How to Sort in Python with the Lambda Function as Key Argument? Read More »

The Pathfinder Graph Algorithm in Python

Knowing the basics is what sets apart the great from the intermediate coders. In other words, a simple and effective way to grow your skills is to learn the basics of computer science. Python Pathfinder Algorithm In this tutorial, you’ll learn about the pathfinder algorithm that recursively determines whether there’s a direct or indirect path …

The Pathfinder Graph Algorithm in Python Read More »

Python Multi-Line Strings

Challenge: How to create a multi-line string in Python? In this tutorial, I’ll show you four methods to create and maintain multi-line strings in Python. The most Pythonic ones are the first two methods with triple single quotes ”’ … ”’ or triple double quotes “”” … “”” that wrap a string across multiple lines. …

Python Multi-Line Strings Read More »

Lambda Calculus in Python

This tutorial introduces an advanced language feature: lambda functions. Lambda functions are rooted in the mathematical area of lambda calculus. One of the pioneers of this area was Alonzo Church. He introduced lambda functions in 1936 even before the appearance of the first computers. Lambda functions exist in a wide range of languages for functional …

Lambda Calculus in Python Read More »

Python — How to Modify a Sequence While Iterating over It?

Modifying a sequence while iterating over it can cause undesired behavior due to the way the iterator is build. To avoid this problem, a simple solution is to iterate over a copy of the list. For example, you’ll obtain a copy of list_1 by using the slice notation with default values list_1[:]. Because you iterate …

Python — How to Modify a Sequence While Iterating over It? Read More »

Python Join Arguments and String Concatenation

Problem: Write a function that joins an arbitrary number of string arguments with a given separator. Example: Given the string arguments “A”, “B”, and “C” and the string separator “-“. Join them to the concatenated string “A-B-C”. Solution: The following code creates a Python function concat() that takes an arbitrary number of arguments, packs them …

Python Join Arguments and String Concatenation Read More »