Python Forum
how to create a nested dict..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to create a nested dict..
#6
Here's how you can build a dictionary on the fly:

#  Author: Larz60+ Nov 22, 2018
import os


class CreateDict:
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

    def new_dict(self, dictname):
        setattr(self, dictname, {})

    def add_node(self, parent, nodename):
        node = parent[nodename] = {}
        return node
    
    def add_cell(self, nodename, cellname, value):
        cell =  nodename[cellname] = value
        return cell

    def display_dict(self, dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1


def testit():
    cd = CreateDict()
    cd.new_dict('CityList')
    boston = cd.add_node(cd.CityList, 'Boston')
    bos_resturants = cd.add_node(boston, 'Resturants')
    spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
    cd.add_cell(spoke, 'Addr1', '89 Holland St')
    cd.add_cell(spoke, 'City', 'Sommerville')
    cd.add_cell(spoke, 'Addr1', '02144')
    cd.add_cell(spoke, 'Phone', '617-718-9463')

    highland = cd.add_node(bos_resturants, 'Highland Kitchen')
    cd.add_cell(highland, 'Addr1', '150 Highland Ave')
    cd.add_cell(highland, 'City', 'Sommerville')
    cd.add_cell(highland, 'ZipCode', '02144')
    cd.add_cell(highland, 'Phone', '617-625-1131')

    print(f'\nCityList Dictionary')
    cd.display_dict(cd.CityList)
    print(f'\nraw data: {cd.CityList}')

if __name__ == '__main__':
    testit()
The test routing has the code to reproduce the sample dictionary I show above in post #2
Output:
Boston Resturants Spoke Wine Bar Addr1: 02144 City: Sommerville Phone: 617-718-9463 Highland Kitchen Addr1: 150 Highland Ave City: Sommerville ZipCode: 02144 Phone: 617-625-1131 raw data: {'Boston': {'Resturants': {'Spoke Wine Bar': {'Addr1': '02144', 'City': 'Sommerville', 'Phone': '617-718-9463'}, 'Highland Kitchen': {'Addr1': '150 Highland Ave', 'City': 'Sommerville', 'ZipCode': '02144', 'Phone': '617-625-1131'}}}}
Reply


Messages In This Thread
how to create a nested dict.. - by wardancer84 - Nov-22-2018, 02:14 PM
RE: how to create a nested dict.. - by Larz60+ - Nov-22-2018, 02:26 PM
RE: how to create a nested dict.. - by wardancer84 - Nov-22-2018, 03:11 PM
RE: how to create a nested dict.. - by Gribouillis - Nov-22-2018, 03:16 PM
RE: how to create a nested dict.. - by wardancer84 - Nov-22-2018, 03:20 PM
RE: how to create a nested dict.. - by Larz60+ - Nov-23-2018, 04:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating nested dict list keys tbaror 2 1,302 Feb-09-2022, 09:37 AM
Last Post: tbaror
  changing key names in nested dict wardancer84 6 2,183 Sep-10-2021, 08:13 AM
Last Post: wardancer84
  Create Dict from multiple Lists with duplicate Keys rhat398 10 4,120 Jun-26-2021, 11:12 AM
Last Post: Larz60+
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,920 Jan-22-2021, 05:43 AM
Last Post: buran
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,488 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Sort a dict in dict cherry_cherry 4 79,068 Apr-08-2020, 12:25 PM
Last Post: perfringo
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,240 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  Easy way to sort a nested dict Alfalfa 3 5,697 Dec-07-2018, 04:12 PM
Last Post: Alfalfa
  Better way to create nested dictionary with defaultdict() x2mlh 8 21,740 Nov-30-2017, 08:10 PM
Last Post: buran
  sorting nested dict according to values merlem 6 17,669 Apr-01-2017, 10:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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