How to Find the Maximum Value in a Python Dict?

There are three problem variants of finding the maximum value in a Python dictionary: In the following, you’ll learn how to solve those in the most Pythonic way: Find Maximum Value & Return Only Value The output is: Find Maximum Value & Return (Key, Value) Pair The output is: Find Maximum Value & Return Only … Read more

How to Sort a Dictionary in Python By Key?

To sort a dictionary by key in Python, use the dictionary comprehension expression {key:dict[key] for key in sorted(dict)} to create a new dictionary with keys in sorted order. You iterate over all keys in sorted order, obtain their associated values with rank[key], and put them into a new dictionary. Python dictionaries preserve the order—even if … Read more

Python Boolean Variables

Python Boolean variables are set to either the True or False keywords. Both keywords require an Upper Case letter—lowercase letters are interpreted as normal variable or function names, so you could set true = True. A variable is of type Boolean if type(variable) results in the output <class ‘bool’>. You can convert any object to … Read more

Python’s __import__() Function — Dynamically Importing a Library By Name

Python’s built-in “dunder” function __import__() allows you to import a library by name. For example, you may want to import a library that was provided as a user input, so you may only have the string name of the library. For example, to import the NumPy library dynamically, you could run __import__(‘numpy’). In this tutorial, … 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 … Read more

Top 10 Git Cheat Sheets

Hello everyone! It is time for another top 10 cheat sheet! Today, I will be presenting the top 10 cheat sheets on Git! Not to be confused with GitHub, Git is a repository for all the changes you have made on your web or software development project. At first Git can be a little confusing, … Read more

Matplotlib Widgets — How to Make Your Plot Interactive With Buttons

This article presents different types of widgets that can be embedded within a matplotlib figure, in order to create and personalize highly interactive plots. Exploiting the matplotlib package .widget(), it is hence possible to create personalized buttons that allows controlling different properties of the graphs that are plotted in the main window. This represents a … 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 … Read more