# Python One-Liners

## 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 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 Sum List of Lists in Python? [Rows]

Problem: Given a list of lists representing a data matrix with n rows and m columns. How to sum over the rows of this matrix? In this article, you’re going to learn different ways to accomplish this in Python. Let’s ensure that you’re on the same page. Here’s a graphical representation of the list of …

## How to Sum List of Lists in Python? [Columns]

Problem: Given a list of lists representing a data matrix with n rows and m columns. How to sum over the columns of this matrix? In this article, you’re going to learn different ways to accomplish this in Python. Let’s ensure that you’re on the same page. Here’s a graphical representation of the list of …

## How to Average a List of Lists in Python?

Problem: You have a list of lists and you want to calculate the average of the different columns. Example: Given the following list of lists with four rows and three columns. You want to have the average values of the three columns: There are three methods that solve this problem. You can play with them … ## How to Remove Empty Lists from a List of Lists in Python?

Short answer: You can remove all empty lists from a list of lists by using the list comprehension statement [x for x in list if x] to filter the list. In the following, you’ll learn about the two methods using list comprehension and the filter() function to remove all empty lists from a list of …

## List Comprehension Python List of Lists

[20-SEC SUMMARY] Given a list of list stored in variable lst. To flatten a list of lists, use the list comprehension statement [x for l in lst for x in l]. To modify all elements in a list of lists (e.g., increment them by one), use a list comprehension of list comprehensions [[x+1 for x …

## How to Convert List of Tuples to List of Lists in Python?

If you’re in a hurry, here’s the short answer: use the list comprehension statement [list(x) for x in tuples] to convert each element in your tuples list to a list. This works also for list of tuples with varying number of elements. But there’s more to it and studying the two main method to achieve …

## How to Convert List of Lists to List of Tuples in Python?

If you’re in a hurry, here’s the short answer: use the list comprehension statement [tuple(x) for x in list] to convert each element in your list to a tuple. This works also for list of lists with varying number of elements. But there’s more to it and studying the two main methods to achieve the …

## How to Convert a List of List to a Dictionary in Python?

For some applications, it’s quite useful to convert a list of lists into a dictionary. Databases: List of list is table where the inner lists are the database rows and you want to assign each row to a primary key in a new dictionary. Spreadsheet: List of list is two-dimensional spreadsheet data and you want …

## How to Convert a List of Lists Into A Single List

Say, you want to convert a list of lists lst=[[1, 2], [3, 4]] into a single list [1, 2, 3, 4]. How to achieve this? There are different options: List comprehension [x for l in lst for x in l] assuming you have a list of lists lst. Unpacking [*lst, *lst] assuming you have a …

## Python Lists filter() vs List Comprehension – Which is Faster?

[Spoiler] Which function filters a list faster: filter() vs list comprehension? For large lists with one million elements, filtering lists with list comprehension is 40% faster than the built-in filter() method. To answer this question, I’ve written a short script that tests the runtime performance of filtering large lists of increasing sizes using the filter() …