How to Find All Palindromes in a Python String?

Coding Challenge πŸ’¬ Challenge: Given a string. How to find all palindromes in the string? For comprehensibility, allow me to quickly add a definition of the term palindrome: πŸ’‘ Definition: A palindrome is a sequence of characters that reads the same backward as forward such as ‘madam’, ‘anna’, or ‘101’. This article wants to give … Read more

How to Filter a Dictionary in Python? (… The Most Pythonic Way)

Problem: Given a dictionary and a filter condition. How to filter a dictionary by … … key so that only those (key, value) pairs in the dictionary remain where the key satisfies the condition? … value so that only those (key, value) pairs remain where the value satisfies the condition? In this tutorial, you’ll learn … Read more

Find Longest Palindrome in a Python String (Easy)

Coding Challenge πŸ’¬ Challenge: Given a string. How to find the longest palindrome in the string? For comprehensibility, allow me to quickly add a definition of the term palindrome: πŸ’‘ Definition: A palindrome is a sequence of characters that reads the same backward as forward such as ‘madam’, ‘anna’, or ‘101’. This article wants to … Read more

How to Check if a Python Set is Empty?

There are three main ways to check if a set is empty: Implicit: Pass the set into a Boolean context without modification. Python will automatically convert it to a Boolean using bool(). Explicit: Pass the set into the bool() Function. Comparison: Compare the set with the empty set like so: my_set == set() Let’s dive … Read more

How to Find the Longest String in a Python Set?

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

How to Find the Longest String in a Python Dictionary?

You’ll never guess the most concise way to solve this problem… πŸ¦„ Coding Challenge πŸ’¬ Challenge: Given a dictionary with string keys. Get the string key with the maximum number of characters, i.e., the longest string. A variant of this problem is to get the length of the longest string — I’ll give you a … Read more

How to Find a Partial String in a Python List?

Problem Formulation πŸ’¬ Challenge: Given a Python list of strings and a query string. Find the strings that partially match the query string. Example 1: Input: [‘hello’, ‘world’, ‘python’] and ‘pyth’ Output: [‘python’] Example 2: Input: [‘aaa’, ‘aa’, ‘a’] and ‘a’ Output: [‘aaa’, ‘aa’, ‘a’] Example 3: Input: [‘aaa’, ‘aa’, ‘a’] and ‘b’ Output: [] … Read more

Python TypeError ‘set’ object is not subscriptable

Minimal Error Example Given the following minimal example where you create a set and attempt to access an element of this set using indexing or slicing: If you run this code snippet, Python raises the TypeError: ‘set’ object is not subscriptable: Why Does the Error Occur? The Python TypeError: ‘set’ object is not subscriptable occurs … Read more