Python Int to String with Leading Zeros

To convert an integer i to a string with leading zeros so that it consists of 5 characters, use the format string f'{i:05d}’. The d flag in this expression defines that the result is a decimal value. The str(i).zfill(5) accomplishes the same string conversion of an integer with leading zeros. Challenge: Given an integer number. … Read more

How To Extract Numbers From A String In Python?

The easiest way to extract numbers from a Python string s is to use the expression re.findall(‘\d+’, s). For example, re.findall(‘\d+’, ‘hi 100 alice 18 old 42’) yields the list of strings [‘100′, ’18’, ’42’] that you can then convert to numbers using int() or float(). There are some tricks and alternatives, so keep reading … Read more

Python Generator Expressions

Short Overview Python Generator Expressions provide an easy-to-read syntax for creating generator objects. They allow you to quickly create an iterable object by using a single line of code. A generator expression is like a list comprehension, but instead of creating a list, it returns an iterator object that produces the values instead of storing … Read more

How to Count the Number of Unique Values in a List in Python?

Problem Statement:Β Consider that you have been given a list in Python. How will you count the number of unique values in the list? Example: Let’s visualize the problem with the help of an example: Given:Β li = [‘a’, ‘a’, ‘b’, ‘c’, ‘b’, ‘d’, ‘d’, ‘a’]Output:Β The unique values in the given list are ‘a’, ‘b’, ‘c’, ‘d’. … Read more

How to Sum the Digits of a Number in Python?

Problem Formulation Given an integer number. How to sum over all digits of this number to compute the crossfoot (cross total)? Consider the following examples: 12 –> 1+2 = 3 123 –> 1+2+3 = 3 244 –> 2+4+4 = 10 981223 –> 9+8+1+2+2+3 = 25 Method 1: sum() with Generator Expression The Python built-in sum(iterable) … Read more

How to Retrieve a Single Element from a Python Generator

This article will show you how to retrieve a single generator element in Python. Before moving forward, let’s review what a generator does. Quick Recap Generators πŸ’‘ Definition Generator: A generator is commonly used when processing vast amounts of data. This function uses minimal memory and produces results in less time than a standard function. … 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

Initialize a Huge Python Dict of Size N (Easy & Quick)

βš”οΈ Programming Challenge: Given an integer n that could be very high (e.g., n=1000000). How to initialize a Python dictionary of size n that is fast, easy, and efficient? Next, you’ll learn the five main ways to solve this and compare their performance at the end of this article. Interestingly, the winner Method 4 is … Read more

If-Then-Else in One Line Python

Quick answer: How to put a simple if-then-else statement in one line of Python code? To put an if-then-else statement in one line, use Python’s ternary operator x if c else y. This returns the result of expression x if the Boolean condition c evaluates to True. Otherwise, the ternary operator returns the alternative expression … Read more

Python One Line Ternary

The most basic ternary operator x if c else y consists of three operands x, c, and y. It is an expression with a return value. The ternary operator returns x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative y. Ternary … Read more

How to Find the Shortest String in a Python Set?

Use Python’s built-in min() function with the key argument to find the shortest string in a set. Call min(my_set, key=len) to return the shortest string in the set using the built-in len() function to determine the weight of each stringβ€”the shortest string has minimum length. πŸ‘‰ Recommended Tutorial: How to Find the Shortest String in … Read more