We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Can you tell me how python carries out list comprehension internally? How come this generates lists in lexicographic increasing order (i.e z increases first, then y, then x)?

It is in lexographical order due to the nature of the loops.
Spaced out it looks somewhat like

for a in range(x+1):
for b in range(y+1):
for c in range(z+1):
if a + b + c != n:
print(stuff is here)

We start at [0,0,0].
Then c will increment to get to [0,0,1]
When c hits [0,0,z], we get [0,1,0] as the next in the loop. This pattern continues and gives us the lexographical ordering required of the output.

## List Comprehensions

You are viewing a single comment's thread. Return to all comments →

Can you tell me how python carries out list comprehension internally? How come this generates lists in lexicographic increasing order (i.e z increases first, then y, then x)?

It is in lexographical order due to the nature of the loops. Spaced out it looks somewhat like

We start at [0,0,0]. Then c will increment to get to [0,0,1] When c hits [0,0,z], we get [0,1,0] as the next in the loop. This pattern continues and gives us the lexographical ordering required of the output.

it's very helpfull to understanding that what is actualy asking

Thank you. This is the a verbose response, but clearly the most elegant to understand what's going on.

yup! i understand.

Thanks for your solution, now we understand how it's word.

Here is my soltion with your sussgestion:

empty_list = [] for a in range ( x+ 1): for b in range ( y + 1): for c in range ( z + 1): #logic code if (a + b + c != n) : empty_list.append([a,b,c])

print(empty_list)

But my program is not working properly.

Thank you, I really wondered on how it works.

really much helpfull in understanding