Search code examples
pythonpython-2.7loopsnested-loopsbreak

Understanding control flow of nested for loops sans break statement


I'm trying to understand now what's happening here, but why the control flow of nested loops in Python work they way they do in this particular scenario:

# let's say that `res` is a list of dictionaries where len(res) == 20

for index, item in enumerate(res):
        print index,item
        for key, value in item.iteritems():
            id = item.get('id')
            print id
            video_asset_ids.append(id)
           # break        

The output of the above nested loops looks like this:

0 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
4416825569001
1 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001
34351747001

etc, etc

Obviously when we add a break statement to the nested loop, the id object is only printed out once, and the output looks like this:

0 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
4416825569001
1 {u'updated_at': u'2012-09-18T22:07:37.027Z', u'account_id': u'2001', u'duration': 232410, u'text_tracks': [], u'images': {u'poster': {u'asset_id': u'34343615001', u'width': None, u'height': None}]}, u'thumbnail': {u'asset_id': u'34343614001', u'width': None, u'height': None}]}}, u'digital_master_id': None, u'custom_fields': {}, u'schedule': {u'starts_at': u'2009-08-18T00:53:17.569Z', u'ends_at': None}, u'id': u'34351747001', u'state': u'ACTIVE', u'cue_points': [], u'sharing': {u'source_id': None, u'to_external_acct': True, u'by_id': None, u'by_reference': False, u'by_external_acct': False}, u'complete': True, u'tags': [u'football', u'nfl network nfl films football', u'tv', u'sports', u'pro', u'entertainment'], u'link': None, u'reference_id': u'1578699', u'geo': None, u'name': u'Video: Inside NFL Films', u'created_at': u'2009-08-18T00:53:17.569Z', u'economics': u'AD_SUPPORTED', u'original_filename': None, u'folder_id': None}
34351747001

etc, etc

So without the break statement, how are these loops functioning in terms of control flow (or on the sack)? Why is it that, without the break statement, the nested loops keeps printing out the same id object a number of times equal to the number of items in the list?


Solution

  • Looping through item.iteritems() goes through every key, value pair in the dictionary, so you are printing the same ID for every pair. You don't need the inner loop at all. Try this:

    for index, item in enumerate(res):
        print index,item
        id = item.get('id')
        print id
        video_asset_ids.append(id)