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!
[range(r) for r in range(3)]ris set to 0,1, and 2. For eachrvalue it makes a list with the length ofr, the values ofrange(r). It's best to test this in an interactive session. Try[c for c in range(3)], etc