-1

If I use:

[(x, y) for x in range(5) for y in range(0, x * 6)]

I get

[(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (2, 11), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (3, 13), (3, 14), (3, 15), (3, 16), (3, 17), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (4, 11), (4, 12), (4, 13), (4, 14), (4, 15), (4, 16), (4, 17), (4, 18), (4, 19), (4, 20), (4, 21), (4, 22), (4, 23)]

Why is the first element (1, 0)?

sajiang
  • 60
  • 6

2 Answers2

4

Because when x is 0, y in range(0, x * 6) becomes y in range(0, 0), which is an empty range.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
Kevin Wheeler
  • 1,331
  • 2
  • 15
  • 24
0

First, let's take a look at what the double for loop means:

[(x, y) for x in range(5) for y in range(0, x * 6)]

is the same as:

answer = []
for x in range(5):
    for y in range(0, x*6):
        answer.append((x,y))

[source]

So in the first iteration of x, x takes the value 0. Therefore, the inner expression becomes

for y in range(0, 0*6)

which is the same as

for i in range(0, 0)

Well, range(0,0) has no elements in it. Therefore, the inner loop never creates elements when x=0

Community
  • 1
  • 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241