Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read aloud Python List Comprehensions?

Tags:

My question is about Python List Comprehension readability. When I come across code with complex/nested list comprehensions, I find that I have to re-read them several times in order to understand the intent.

Is there an intuitive way to read aloud list comprehensions? Seems like I should start "reading" from the middle, then read the if conditions (if any), and read the expression last.

Here's how I would read the follow line of code aloud, in order to understand it:

[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] 

"For each element in List x, and each element in List y, if the two elements are not the same, create a list of tuples."

Two examples that I am struggling with: How would you read the following List Comprehensions aloud?

  1. From another question in Stack Overflow: [x for b in a for x in b]

  2. Python docs has this example: [[row[i] for row in matrix] for i in range(4)]

Any suggestions or pointers for ways to read aloud list comprehensions such that the intention becomes clearer is much appreciated.

like image 930
Ram Narasimhan Avatar asked Jan 30 '12 09:01

Ram Narasimhan


People also ask

How do list comprehensions work in Python?

List comprehensions provide us with a simple way to create a list based on some sequence or another list that we can loop over. In python terminology, anything that we can loop over is called iterable. At its most basic level, list comprehension is a syntactic construct for creating lists from existing lists.

Are list comprehensions better than for loops?

Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.

What are the comprehensions in Python?

In this article, I have introduced all the 4 types of comprehensions in Python — List, Dictionary, Set and Generator Comprehension. Most of the time, they are very elegant, concise and even can improve the readability of our code.

Is it possible to use list comprehension with a string?

List comprehension in Python is an easy and compact syntax for creating a list from a string or another list. It is a very concise way to create a new list by performing an operation on each item in the existing list. List comprehension is considerably faster than processing a list using the for loop.


2 Answers

I usually unfold it in my mind into a generating loop, so for example

[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] 

is the list comprehension for the generator

for x in [1,2,3]:     for y in [3,1,4]:         if x != y:             yield (x, y) 

Example #1

[x for b in a for x in b] is the comprehension for

for b in a:     for x in b:         yield x 

Example result for a = [[1,2,3],[4,5,6]]: [1, 2, 3, 4, 5, 6]


Example #2

[[row[i] for row in matrix] for i in range(4)] (note the inner expression is another comprehension!):

for i in range(4):     yield [row[i] for row in matrix] 

which is unfolded

for i in range(4):     l = []      for row in matrix:         l.append(row[i])      yield l 
like image 105
AndiDog Avatar answered Oct 25 '22 14:10

AndiDog


"Construct a list of X's based on Y's and Z's for which Q is true."

like image 27
Amber Avatar answered Oct 25 '22 13:10

Amber