Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with nested dictionary
#2
I added code tags for you.
It's so much easier to read a dictionary if written like:
bond0: {
    'MII Status:': 'up',
    'Aggregator ID:': '2',
    'Slave Interfaces': {
        'p1p1': {
            'MII Status': 'up',
            'Permanent HW addr': '9c:dc:71:45:eb:80',
            'MII Status': up
        },
        'p4p1': {
            'MII Status' : 'up',
            'Permanent HW addr': '9c:dc:71:4d:80:20',
            'MII Status': up
        }
    }
}
you can use something like the following to traverse a nested dictionary:
def display_dict(thedict):
    for key, value in thedict.items():
        if isinstance(value, dict):
            print(f'{key}:')
            self.display_dict(value)
        else:
            print(f'    {key}: {value}')

display_dict(bond0)
This also corrects errors you had in your definition
Reply


Messages In This Thread
Help with nested dictionary - by Anidil - Sep-24-2018, 10:02 PM
RE: Help with nested dictionary - by Larz60+ - Sep-24-2018, 10:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 975 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Nested dictionary acting strange Pedroski55 2 2,199 May-13-2021, 10:37 PM
Last Post: Pedroski55
  format the output from a nested dictionary. nostradamus64 9 4,825 May-03-2021, 04:45 PM
Last Post: nostradamus64
Lightbulb Python Nested Dictionary michaelserra 2 2,720 Apr-18-2021, 07:54 AM
Last Post: michaelserra
  nested dictionary rkpython 7 3,346 May-29-2020, 11:13 AM
Last Post: rkpython
  Nested Dictionary/List tonybrown3 5 3,258 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help: for loop with dictionary and nested lists mart79 1 1,920 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Transforming nested key-tuples into their dictionary values ClassicalSoul 4 2,779 Apr-11-2020, 04:36 PM
Last Post: bowlofred
  How to change value in a nested dictionary? nzcan 2 5,858 Sep-23-2019, 04:09 PM
Last Post: nzcan
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,428 Jul-02-2019, 02:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020