'Iterating through Panda Dictionary

Can someone help explain how I would be able to loop through this dictionary and access'App name'? I've tried assigning all of this to a variable, but it's still having a hard time accepting 'App Name'.

{
'APPID1':
{'updated_cols': ['App Name', 'Platform Leader', 'AppOne Business Group'],         'new_vals': ['my new app', 'new pl', 'new bg'],             'old_vals': ['my old app', 'old pl', 'old bg']       }
,
'APPID2':
{'updated_cols': \['App Name', 'Platform Leader', 'AppOne Business Group'\],         'new_vals': \['my new app', 'new pl', 'new bg'\],             'old_vals': \['my old app', 'old pl', 'old bg'\]       }
....
}

I have tried setting this dictionary to a variable and looping through, but the only thing that is returned is "APPID1" and "APPID2".



Solution 1:[1]

I took some liberty to guess what you might be interested to do and created following as a reference


all_records = dict({
    'APPID1': dict({'updated_cols': ['App Name', 'Platform Leader', 'AppOne Business Group'],
                    'new_vals': ['my new app', 'new pl', 'new bg'], 
                    'old_vals': ['my old app', 'old pl', 'old bg']
                    }),
    'APPID2': dict({'updated_cols': ['App Name', 'Platform Leader', 'AppOne Business Group'],
                    'new_vals': ['my new app', 'new pl', 'new bg'],
                    'old_vals': ['my old app', 'old pl', 'old bg']
                    }),
    })

for key, record_dict in all_records.items():
  print('---'* 12 + ' ' + key)
  print('|'.join(record_dict['updated_cols']))
  print('|'.join(record_dict['old_vals']))
  print('|'.join(record_dict['new_vals']))


output

------------------------------------ APPID1
App Name|Platform Leader|AppOne Business Group
my old app|old pl|old bg
my new app|new pl|new bg
------------------------------------ APPID2
App Name|Platform Leader|AppOne Business Group
my old app|old pl|old bg
my new app|new pl|new bg

I would suggest you to go through Python basics once more. Learn a bit more about lists, dict and set data types.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sam