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
0

2 Answers 2

3

I would use itertools.product

from itertools import product
for x, y in product(range(500), range(300)):
    print x, y
Sign up to request clarification or add additional context in comments.

Comments

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))]

2 Comments

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.
Yeah, I changed my answer, since zip doesn't do what you asked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.