Question

What are the ramifications for the different placement of my variable initialization shown below? Bit of a slow night mentally :/

for row in range(0,3):
    for column in range(0,3):
        block_set = set()

for row in range(0,3):
    block_set = set()
    for column in range(0,3):
Was it helpful?

Solution

Of course there will be differences. The first version creates a new set for each iteration of the inner loop, whereas the second version will create a single set for each iteration of the outer loop.

To put it another way: the second version will create a set that will be shared between all iterations of the inner loop, whereas the first version creates a new set each time, and all data added to the set will be lost. I can't tell for sure without seeing the rest of the code, but it's almost certain that one of the two versions is wrong (it all depends on what you actually want to do.)

OTHER TIPS

for row in range(0,3):
    for column in range(0,3):
        block_set = set() # block_set gets reset to an empty set on every iteration of the inner, 'for column' loop.

for row in range(0,3):
    block_set = set()  # block_set will only be reset once you've finished iterating over the 'for column' loop below, and moved to the next step of the outer, 'for row' loop.
    for column in range(0,3):
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top