Python One-Liners

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 »

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? [Rows] Read More »

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 Sum List of Lists in Python? [Columns] Read More »

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 Tuples to List of Lists in Python? Read More »

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 List of Lists to List of Tuples in Python? Read More »

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 List to a Dictionary in Python? Read More »

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() …

Python Lists filter() vs List Comprehension – Which is Faster? Read More »