1

Instead of doing this:

for x in range(500):
    for y in range(300):
        print x,y

How can i do something like this?

for x,y in range(500),range(300):
    print x,y
Rayden
  • 160
  • 1
  • 9

2 Answers2

3

I would use itertools.product

from itertools import product
for x, y in product(range(500), range(300)):
    print x, y
mway
  • 615
  • 5
  • 14
2

Using list comprehension:

pairs = [ (i,j) for i in range(300) for j in range(300) ]
print pairs

This is the test I have run:

print [ (i,j) for i in range(3) for j in range(3) ]

output

[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

Using itertools' product

print [ (i,j) for i, j in itertools.product(range(300), range(300))]
boaz_shuster
  • 2,825
  • 20
  • 26
  • doesn't zip only work for lists with the same size? Also I prefer it being in the format i said because I need to do a lot of stuff there and list comprehension is a little bit limited since i can only put one line of code. – Rayden Jun 02 '15 at 21:49
  • Yeah, I changed my answer, since `zip` doesn't do what you asked. – boaz_shuster Jun 02 '15 at 21:51