Complex Data Structure

When dealing with very large output data structure , and need to extract data from it :

Example :

Devices = {
‘sw1’: {
‘ip_addr’: ‘10.10.10.1’,
‘username’: ‘admin’,
‘password’: ‘cisco’,
‘bgp_peer’: ‘10.10.20.1’, ‘10.10.30.1’, ‘10.10.40.1’},
‘sw2’: {
‘ip_addr’: ‘10.10.10.2’,
‘username’: ‘admin’,
‘password’: ‘cisco’,},
‘sw3’: {
‘ip_addr’: ‘10.10.10.3’,
‘username’: ‘admin’,
‘password’: ‘cisco’,}
}

So to drill down to get the information :

devices.keys()
would return the keys of that dictionary ( that can return nested dictionary )
dict_keys([‘sw1’, ‘sw2’, ‘sw3’])
devices[‘sw1’].keys()
dict_keys([‘ip_addr’, ‘username’, ‘password’, ‘bgp_peer’])
devices[‘sw1’][bgp_peer]
[‘10.10.20.1’, ‘10.10.30.1’, ‘10.10.40.1’]
devices[‘sw1’][bgp_peer][2]
‘10.10.40.1’
devices[‘sw1’][username]
‘admin’

Leave a Comment