0

I have a dictionary of dictionaries and want to get the Primary keys of it only. There is no way to eliminate the other keys by name.

That is an example dictionary:

{
'default': {
    'routes': '3',
    'paths': '3',
    'best_paths': {
        'broadcast': '3'
    },
    'backup_paths': {}
        },
'management': {
    'routes': '15',
    'paths': '15',
    'best_paths': {
        'local': '1',
        'direct': '1',
        'static': '1',
        'eigrp': '5'
    },
    'backup_paths': {}
        },
'test1': {
    'routes': '260',
    'paths': '275',
    'best_paths': {
        'local': '16',
        'direct': '16',
        'static': '1',
        'ospf': '33',
    },
    'backup_paths': {}
    },
}

The number of keys could be different. But the depths of dictionaries is always the same.

My expected result would be something like this

key_list = 'default', 'management', 'test1' 

The answer of the question 'python JSON only get keys in first level' is not working.

1 Answers1

1

Hi Keys function will give required output

dic={
'default': {
    'routes': '3',
    'paths': '3',
    'best_paths': {
        'broadcast': '3'
    },
    'backup_paths': {}
        },
'management': {
    'routes': '15',
    'paths': '15',
    'best_paths': {
        'local': '1',
        'direct': '1',
        'static': '1',
        'eigrp': '5'
    },
    'backup_paths': {}
        },
'test1': {
    'routes': '260',
    'paths': '275',
    'best_paths': {
        'local': '16',
        'direct': '16',
        'static': '1',
        'ospf': '33',
    },
    'backup_paths': {}
    },
}
print (dic.keys())
Sandeep Lade
  • 1,865
  • 2
  • 13
  • 23