Top 13 Python Tricks for Data Analysis

This article focuses on analyzing the coronavirus dataset using Python language. We are not using any of the Python data analysis libraries. Instead, we’ll use our raw Python skills to write a function, slicing, and indexing. Also, we’ll use Python arithmetic operators such as sum() and division. Finally, we’ll use a lambda expression to perform … Read more

List Head and Tail in One Line Python

❗ Problem Formulation: How to assign the first list element to the variable head and the remaining elements to the variable tail? Let’s have a look at the two most Pythonic solutions to this one-liner programming challenge! 🙂 Method 1: Unpacking and Multiple Assignment Given a list. The most Pythonic way to unpack the first … Read more

Python Dict Length of Values

This article addresses two problems: Given is a dictionary and a single key. How to get the length of the value associated with the key in the dictionary? Given is a dictionary. How to get the total length, summing over the length of all values in the dictionary? Let’s dive into these two problems and … Read more

How To Apply A Function To Each Element Of A Dictionary?

This article shows you how to apply a given function to each element of a Python dictionary. The most Pythonic way to apply a function to each element of a Python dict is combining the dictionary comprehension feature and the dict.items() method like so: {k:f(v) for k,v in dict.items()} Note: All the solutions provided below … Read more

How To Apply A Function To Each Element Of A Tuple?

This article shows you how to apply a given function to each element of a tuple. The best way to apply a function to each element of a tuple is the Python built-in map(function, iterable) function that takes a function and an iterable as arguments and applies the function to each iterable element. An alternate … Read more

How to Calculate the Edit Distance in Python?

Motivation Type “helo world” into your Google search bar and Google will ask you: “Did you mean: hello world”. How is this done? A simple method to detect these typos is the Levenshtein distance (also called edit distance). In fact, Google’s algorithm seems to use some variant of it. (source) By studying this article, you’ll … Read more

Iterative vs. Recursive Binary Search Algorithms in Python

In this article, you’ll learn about a basic algorithm, every computer scientist must know: the binary search algorithm. I have drawn the code from my NoStarch programming introductory book Python One-Liners: Applications Binary Search The algorithm has important practical applications in many basic data structures such as sets, trees, dictionaries, bags, bag trees, bag dictionaries, … Read more

Python Nested Multiple Ternary Operators

In which order does the nested ternary operator evaluate its conditions? Short Answer: The nested ternary operator ‘1’ if x else ‘2’ if y else ‘3’ evaluates the condition from left to right, i.e., ‘1’ if x else (‘2’ if y else ‘3’). In short, first condition first! Problem Formulation Given a nested ternary operator … Read more

The Shortest Quicksort Algorithm in Python

Quicksort is not only a popular question in many code interviews – asked by Google, Facebook, and Amazon – but also a practical sorting algorithm that is fast, concise, and readable. Because of its beauty, you won’t find many introductions to algorithms that don’t discuss the Quicksort algorithm. In this one-liner tutorial, you’ll learn about … Read more