Python List

Python Join List with Underscore [The Most Pythonic Way]

The ‘_’.join(list) method on the underscore string ‘_’ glues together all strings in the list using the underscore string as a delimiter—and returns a new string. For example, the expression ‘_’.join([‘a’, ‘b’, ‘c’]) returns the new string ‘a_b_c’. Definition and Usage The string.join(iterable) method joins the string elements in the iterable to a new string …

Python Join List with Underscore [The Most Pythonic Way] Read More »

Python List to Set Conversion [Interactive Guide]

Do you have a list but you want to convert it to a Python set? No problem! Use the set(…) constructor and pass the list object as an argument. For example, if you have a list of strings friends, you can convert it to a set using the call set(friends). Here’s an example code snippet: …

Python List to Set Conversion [Interactive Guide] Read More »

How to Add an Element to a Python List at an Index?

To add an element to a given Python list, you can use either of the three following methods: Use the list insert method list.insert(index, element). Use slice assignment lst[index:index] = [element] to overwrite the empty slice with a list of one element. Use list concatenation with slicing lst[:2] + [‘Alice’] + lst[2:] to create a …

How to Add an Element to a Python List at an Index? Read More »

How to Find the Minimum of a List of Lists in Python?

Short answer: To find the minimal list in a list of lists, you need to make two lists comparable. How? With the key argument of the min() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the smallest numerical value is …

How to Find the Minimum of a List of Lists in Python? Read More »

The Most Pythonic Way to Check if a Python String Contains Another String? (Tutorial + Video)

How to check if a Python string s1 contains another string s2? There are two easy ways to check whether string s1 contains string s2: Use the expression s2 in s1, or Use the find method s1.find(s2). You can try both methods in our interactive Python shell (just click “Run” to execute the code in …

The Most Pythonic Way to Check if a Python String Contains Another String? (Tutorial + Video) Read More »

How to Find the Max of a List of Lists in Python?

Short answer: To find the maximal list in a list of lists, you need to make two lists comparable. How? With the key argument of the max() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the largest numerical value is …

How to Find the Max of a List of Lists in Python? Read More »

Python List of Lists Group By – A Simple Illustrated Guide

This tutorial shows you how to group the inner lists of a Python list of lists by common element. There are three basic methods: Group the inner lists together by common element. Group the inner lists together by common element AND aggregating them (e.g. averaging). Group the inner lists together by common element AND aggregating …

Python List of Lists Group By – A Simple Illustrated Guide Read More »

How to Filter a List of Lists in Python?

Short answer: To filter a list of lists for a condition on the inner lists, use the list comprehension statement [x for x in list if condition(x)] and replace condition(x) with your filtering condition that returns True to include inner list x, and False otherwise. Lists belong to the most important data structures in Python—every …

How to Filter a List of Lists in Python? Read More »