Rajeev
Rajeev

Reputation: 1061

Python - Pass index value dynamically

I am reading a deeply nested dictionary called response and getting a formatted output

 response = {u'ResultSet': {u'Rows': [
{u'Data': [{u'VarCharValue': u'Table_name'}, {u'VarCharValue': u'Validation_Scenario'}, {u'VarCharValue': u'No_of_Records'}, {u'VarCharValue': u'Result'}]}, 
{u'Data': [{u'VarCharValue': u'ABC'}, {u'VarCharValue': u'01_scenario2'}, {u'VarCharValue': u'100'}, {u'VarCharValue': u'FAIL'}]}, 
{u'Data': [{u'VarCharValue': u'ABC'}, {u'VarCharValue': u'02_scenario1'}, {u'VarCharValue': u'200'}, {u'VarCharValue': u'FAIL'}]}, 
{u'Data': [{u'VarCharValue': u'ABC'}, {u'VarCharValue': u'03_scenario3'}, {u'VarCharValue': u'300'}, {u'VarCharValue': u'PASS'}]}, 
{u'Data': [{u'VarCharValue': u'ABC'}, {u'VarCharValue': u'04_scenario4'}, {u'VarCharValue': u'400'}, {u'VarCharValue': u'PASS'}]}]}}

print (("{0[ResultSet][Rows][0][Data][0][VarCharValue]}".format(response)),
       ("{0[ResultSet][Rows][0][Data][1][VarCharValue]}".format(response)),
       ("{0[ResultSet][Rows][0][Data][2][VarCharValue]}".format(response)),
       ("{0[ResultSet][Rows][0][Data][3][VarCharValue]}".format(response))) 

Output: ('Table_name', 'Validation_Scenario', 'No_of_Records', 'Result')

Now when i want to loop through the indexes [0][Data] from 0 to 4 i am getting Python - TypeError: list indices must be integers, not str

for i in range(4):
     print (("{0[ResultSet][Rows][i][Data][0][VarCharValue]}".format(response)),
            ("{0[ResultSet][Rows][i][Data][1][VarCharValue]}".format(response)),
            ("{0[ResultSet][Rows][i][Data][2][VarCharValue]}".format(response)),
            ("{0[ResultSet][Rows][i][Data][3][VarCharValue]}".format(response))) 

Error :TypeError: list indices must be integers, not str

I understand that since there is " " i is treated as a string but i wanted to know how can i pass the index values dynamically?

Upvotes: 1

Views: 904

Answers (1)

Barmar
Barmar

Reputation: 781716

Variables aren't expanded inside strings. If you really want to do this with format strings, you have to construct it dynamically:

for i in range(4):
     print ((("{0[ResultSet][Rows][" + str(i) + "][Data][0][VarCharValue]}").format(response)),
            (("{0[ResultSet][Rows][" + str(i) + "][Data][1][VarCharValue]}").format(response)),
            (("{0[ResultSet][Rows][" + str(i) + "][Data][2][VarCharValue]}").format(response)),
            (("{0[ResultSet][Rows][" + str(i) + "][Data][3][VarCharValue]}").format(response)))

or:

for i in range(4):
     print ((("{0[ResultSet][Rows][%d][Data][0][VarCharValue]}" % i).format(response)),
            (("{0[ResultSet][Rows][%d][Data][1][VarCharValue]}" % i).format(response)),
            (("{0[ResultSet][Rows][%d][Data][2][VarCharValue]}" % i).format(response)),
            (("{0[ResultSet][Rows][%d][Data][3][VarCharValue]}" % i).format(response))) 

But it would be simpler to just access the nested element in code, not the format string:

for i in range(4):
    print (response['ResultSet']['Rows'][i]['Data'][0]['VarCharValue'],
           response['ResultSet']['Rows'][i]['Data'][1]['VarCharValue'],
           response['ResultSet']['Rows'][i]['Data'][2]['VarCharValue'],
           response['ResultSet']['Rows'][i]['Data'][3]['VarCharValue'])

Upvotes: 2

Related Questions