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 Randomly Sample from a Python List?

In data science, you will need to learn how to generate random numbers, which in Python, we can do with the random module. In this tutorial, you’ll learn how to solve the following programming challenge: βš”οΈ Challenge: How to do random sampling from a list in Python? Without further ado, let’s dive right into it! … 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

Python Find Longest List in List

Problem Formulation πŸ’¬ Programming Challenge: Given a list of lists (nested list). Find and return the longest inner list from the outer list of lists. Here are some examples: [[1], [2, 3], [4, 5, 6]] πŸ‘‰ [4, 5, 6] [[1, [2, 3], 4], [5, 6], [7]] πŸ‘‰ [1, [2, 3], 4] [[[1], [2], [3]], [4, … Read more

How to Apply a Function to a Python List

Problem Formulation and Solution Overview This article will show you how to apply a function to a List in Python. To make it more interesting, we have the following running scenario: As a Python assignment, you have been given a List of Integers and asked to apply a function to each List element in various … 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

(Solved) Python TypeError: ‘float’ object is not subscriptable

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