2

I'm trying to understand the following snippet of python code:

lst = [[c for c in range(r)] for r in range(3)] #line1
for x in lst:                                   #line2
    for y in x:                                 #line3
        if y < 2:                               #line4
            print('*', end='')                  #line5

I know what the functions like range(3) mean by its own, but I don't get the context. It's a bit complicated to see this nested piece of code. The first line with the 'lst' is most confusing. Why is the first line producing the following output:

[[], [0], [0, 1]]

and how does line2 and line3 works together? Thanks in advance for your answer. Every idea is welcome!

4
  • just a nested list comprehension that could be written really way more simply [range(r) for r in range(3)] Commented Dec 11, 2019 at 20:56
  • this is a list comprehension (the first line). Try to add prints to understand better what's going on, or use this visualizer Commented Dec 11, 2019 at 20:58
  • It's not a real-world example of code. It's just an exam question, so I try to get a better understanding of it. Commented Dec 11, 2019 at 20:58
  • 1
    r is set to 0,1, and 2. For each r value it makes a list with the length of r, the values of range(r). It's best to test this in an interactive session. Try [c for c in range(3)], etc Commented Dec 11, 2019 at 20:59

5 Answers 5

1

Re

"The first line with the 'lst' is most confusing.":

Wherever you see [ ...for...] you have what's called a "list comprehension." This is a way to build up a list based on a one-line-loop description of the elements. For example:

list1 = [letter for letter in 'abcd'] 

and

list2 = []
for letter in 'abcd':
    list2.append(letter)

yield identical lists list1 and list2

In your case, you have two sets of [] and two for statements, so you have a list comprehension inside a list comprehension: so the result is not just a list but a nested list.

Re

"and how does line2 and line3 works together?"

Line2 iterates through all the items in your list lst. But each of those items is also a list, because you have a nested list. So line3 iterates through each item in that inner list.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! I got it :)
Awesome! Glad to hear it!
0

In that nested list comprehension, r begins with a value of 0, so the inside list evaluates to [c for c in range(0)], which is []. When r is 1, it evaluates to [c for c in range(1)], which is [0]. When c is 2, you have [c for c in range(2)], which is [0,1]. When these are generated within the outer list comprehension, these are returned as list elements of the list. So you have a list of lists in lst.

The for loop then iterates over each of these lists in line 2. Line 3 then iterates over the integer elements within each list.

Comments

0
lst = [[c for c in range(r)] for r in range(3)]

this is a nested list comprehension.

But here it could be simplified as we don't need a list of lists, just a list of range objects so we can iterate on them. So

lst = [range(r) for r in range(3)]

is way simpler.

And while we're at it, why creating a list comprehension at all? Just remove it, and use a classic loop

for r in range(3):
    for y in range(r):
        if y < 2:
            print('*', end='')

Comments

0

The snippet creates a LIST of LISTS where size (and item's value actually) depends on the index in the first level list.

The snippet is the same as

result = []
for k in range(3):
    result.append([])
    for v in range(k):
        result[k].append(v)

print(result)
=>>>
[[], [0], [0, 1]]

Comments

0

The first line is equivalent to

lst = [list(range(r)) for r in range(3)]

It is a list comprehension which generates a list containing list up to r-1. list(range(r)) generates [0, 1, 2, ..., r-1], and because the r variable gets the values 0, 1 and 2 in the list comprehension, it means it will generate a list with the lists [] (r=0), [0], (r=1) and [0, 1] (r=2).

Line 2 iterates over the lists of lst, and line 3 iterates over the values of each list. Therefore, these two lines jointly iterate over all the numbers in lst.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.