1

I have the following code:

def odraw(oposlist, osizelist):
    for opos in oposlist and osize in osizelist:
        pygame.draw.rect(screen, black, (opos[0], opos[1], osize[0], osize[1]))

How to rephrase the second line? How it is written right now obviously does not match the Python syntax.

2 Answers 2

3

If you are looking for pairwise iteration, use zip:

for opos, osize in zip(oposlist, osizelist):

If, however, you want the cartesian product (pair every element in oposlist with every element in osizelist), use itertools.product ...

from itertools import product
# ...
for opos, osize in product(oposlist, osizelist):

... or simply nest loops:

for opos in oposlist:
    for osize in oposlist:
        # do stuff
Sign up to request clarification or add additional context in comments.

Comments

2

You can do:

for i in range(len(oposlist)):
    # Refer oposlist and osizelist like
    oposlist[i]
    osizelist[i]

Tbh, other answer is better :-)

1 Comment

Not always better ;-) E.g., in list comprehension I'd prefer iterating over indexes cause it's just more compact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.