Python Forum
Dynamic Allocation of Nested Dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic Allocation of Nested Dictionaries
#1
Hello,

Here's a class for dynamically allocating a dictionary with an example
"""
Dynamic nested dictionary
Author: Larz60+
"""
#
import collections


class DynamicNestedDict:
    def dynamic_nested_dict(self):
        return collections.defaultdict(self.dynamic_nested_dict)


# Example of usage
if __name__ == '__main__':
    keylist = ['bind', 'application level', 'binding']
    loc = [20, 96, 100, 101, 102, 104, 105, 115, 193, 434, 546]

    dd = DynamicNestedDict()
    ddict = dd.dynamic_nested_dict()

    x = 'ddict'
    for n in range(len(keylist)):
        x = '{}[{}]'.format(x, repr(keylist[n]))
    x = x + ' = loc'
    exec(x)

    print("ddict['bind']['application level']['binding']: {}"
          .format(ddict['bind']['application level']['binding']))
Any improvements appreciated


Messages In This Thread
Dynamic Allocation of Nested Dictionaries - by Larz60+ - Oct-08-2016, 12:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Dynamic nested Dictionaries Larz60+ 8 9,222 Nov-26-2018, 12:58 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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