kashive
kashive

Reputation: 1356

python code does not execute outer loop after finishing with inner loop, but only for the first element

Background: I have a Hash that I am iterating through to print it in a nice format.

Sample Hash

Final = {
    'sf_market_flash_subscribers': [
        {'38': './BLMetricsSql.sql'}
    ], 
    'vw_owner_product_bls_only': [
        {'31': './BLMetricsSql.sql'}, 
        {'39': './BLMetricsSql.sql'}, 
        {'62': './BLMetricsSql.sql'}, 
        {'64': './BLMetricsSql.sql'}
    ]
}

My Code

def printFinal(final):
    for key, value in final.iteritems():
        print key
        print value
        line_num=""
        path=""
        uniqueTables=[]
        for line_and_path in value:
            path=line_and_path.values()[0]
            uniqueTables.append(path)
            print uniqueTables
    print uniqueTables
    uniqueTables=list(set(uniqueTables))
    print uniqueTables
    for unique_path in uniqueTables:
        print unique_path
        for line_and_path in value:
            if line_and_path.values()[0]==unique_path.rstrip():
                line_num = line_and_path.keys()[0]
                print line_num

Output of the above code

sf_market_flash_subscribers
[{'38': './BLMetricsSql.sql'}]
['./BLMetricsSql.sql']
vw_owner_product_bls_only
[{'31': './BLMetricsSql.sql'}, {'39': './BLMetricsSql.sql'}, {'62': './BLMetricsSql.sql'}, {'64': './BLMetricsSql.sql'}]
['./BLMetricsSql.sql']
['./BLMetricsSql.sql', './BLMetricsSql.sql']
['./BLMetricsSql.sql', './BLMetricsSql.sql', './BLMetricsSql.sql']
['./BLMetricsSql.sql', './BLMetricsSql.sql', './BLMetricsSql.sql', './BLMetricsSql.sql']
['./BLMetricsSql.sql', './BLMetricsSql.sql', './BLMetricsSql.sql', './BLMetricsSql.sql']
['./BLMetricsSql.sql']
./BLMetricsSql.sql
31
39
62    
64

Look at the first 4 lines of output

The loop prints the key and value in line 1 and 2 respectively Then it prints the current state of the unique table inside the loop After that, BOOM all the remaining code does not get executed and it goes and loops over the second element in the final hash vw_owner_product_bls_only and then starts executing it. However, for this element all the code after the inner loop gets executed and I get the desired output.

So, why is the rest of the code not running only for the first element in the final hash? Is it because the value in the hash only has one element?

Upvotes: 1

Views: 984

Answers (2)

Peter Westlake
Peter Westlake

Reputation: 5036

You initialise uniqueTables every time round the loop, so the code outside the loop only sees the most recent value, which is the second element. The initialisation needs to be at the outermost level:

def printFinal(final):
    uniqueTables = []
    for key, value in final.iteritems():
        # etc
    print uniqueTables
    uniqueTables=list(set(uniqueTables))
    # etc

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798656

Because your indentation is incorrect. If you want the code to run inside the loop, then it has to be indented further than the statement that starts the loop.

Upvotes: 1

Related Questions