(Fixed) Python TypeError ‘bool’ object is not subscriptable

Problem Formulation Consider the following minimal example where a TypeError: ‘bool’ object is not subscriptable occurs: This yields the following output: Solution Overview Python raises the TypeError: ‘bool’ object is not subscriptable if you use indexing or slicing with the square bracket notation on a Boolean variable. However, the Boolean type is not indexable and … Read more

How to Update Python?

Python gets updated roughly once per year with a major update and security fixes. So, Python’s update cycle is 12 months. The end-of-life, i.e., the period during which one Python version is supported, is normally set to 5 years. The following graphic shows a great screenshot of supported and unsupported Python versions at the time … 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

How to Stop a For Loop in Python

Python provides three ways to stop a for loop: The for loop ends naturally when all elements have been iterated over. After that, Python proceeds with the first statement after the loop construct. The keyword break terminates a loop immediately. The program proceeds with the first statement after the loop construct. The keyword continue terminates … 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

3 Best Ways to Generate a Random Number with a Fixed Amount of Digits in Python

Coding Challenge βš”οΈ Challenge: Given an integer d representing the number of digits. How to create a random number with d digits in Python? Here are three examples: my_random(2) generates 12 my_random(3) generates 389 my_random(10) generates 8943496710 I’ll discuss three interesting methods to accomplish this easily in Python—my personal favorite is Method 2! Shortest Solution … Read more

Python Find Shortest List in Dict of Lists

Problem Formulation πŸ’¬ Programming Challenge: Given a dictionary where the values are lists of varying sizes. Find and return the shortest list! Here’s an example: Also, you’ll learn how to solve a variant of this challenge. πŸ’¬ Bonus challenge: Find only the key that is associated with the shortest list in the dictionary. Here’s an … Read more

Python Find Longest List in Dict of Lists

Problem Formulation πŸ’¬ Programming Challenge: Given a dictionary where the values are lists of varying sizes. Find and return the longest list! Here’s an example: Also, you’ll learn how to solve a variant of this challenge. πŸ’¬ Bonus challenge: Find only the key that is associated with the longest list in the dictionary. Here’s an … Read more

How to Check If a List of ENS (.eth) Domains are Available? (Python)

You can check programmatically in Python whether a certain ENS ‘.eth‘ domain name is available by using the urlopen() function from urllib.request module to access the URL ‘https://etherscan.io/enslookup-search?search=example.eth’, replacing the URL suffix ‘example.eth’ with your desired domain. In the following example code snippet, I show how you could check a list of names, one domain … Read more