Is nested dictionary in design ok?
Asked Answered
D

6

5

My data is structured in a way that I ended up creating a nested dictionary in my design like:

my_dict = {"a": {"b": {"c":"I am c"}}}   
my_dict["a"]["b"]["c"]

Is it usual! or we have some other better alternatives (using objects!)?

Diplomacy answered 19/2, 2010 at 18:41 Comment(3)
The code you posted isn't indicative of a nested dict.Kelikeligot
I though people will understand so just accessed my nested dictionary!Diplomacy
I think he means a[b][c]Denaturalize
K
9

There is nothing inherently wrong with nested dicts. Anything can be a dict value, and it can make sense for a dict to be one.

A lot of the time when people make nested dicts, their problems could be solved slightly more easily by using a dict with tuples for keys. Instead of accessing a value as d[a][b][c], then, the value would be accessed as d[a, b, c]. This is often easier to set up and work with.

Kelikeligot answered 19/2, 2010 at 18:46 Comment(6)
Tuples also have significantly less overhead than dictionaries.Katha
@Vishal, You would simply access an element like d[a, b, c] or assign to it like d[a, b, c] = 4. This is after initializing it as an empty dict {} or with some values like {(1,2,3): 4, (5,6,7): 8}.Kelikeligot
@Max S., When indexing, the parens there are optional and would usually be omitted.Kelikeligot
@Mike Graham, yeah, after all it's always the commas that make a tuple, not the parens, but I think it was clearer to show the tuple involvement that way.Lindholm
@Mike: What's the difference if I am using string "a-b-c" as a key instead of (a, b, c)?Diplomacy
@Vishal, In one case you are using a string like "a-b-c" and in the other you are using the tuple (a, b, c). The latter case is often useful when you have three Python objects, the combination of which in that order defines what you want to look up. This makes it a somewhat suitable replacement for d[a][b][c].Kelikeligot
T
8

You could use a tuple to store your values in a flat dictionary:

d = {}
d[a, b, c] = e

It all depends on what you are doing, but remember that The Zen of Python says that flat is better than nested :)

Tris answered 19/2, 2010 at 18:46 Comment(0)
H
1

At first they may seem like a good idea, but usually you'll find yourself needing more information/functionality from them. If that happens, your first reaction may be "I need more hashes", but all this can be avoided by using a simpler hierarchy ... it would be easier too.

Hartfield answered 19/2, 2010 at 19:13 Comment(0)
D
0

I would not build complex data structures like that in the code itself. There is too great a risk of making a mistake with punctuation, and anyone reading the code will tend to get confused by this. Far better to put your constant data in some simple form, with no nesting of data structures. Then if you need to have a nested dictionary in the app, build it with code.

a = {"a":"I am a"}
b = {"b":"I am b"}
c = {"c":"I am c"}
mydict = {}
mydict["a"] = a
mydict["b"] = b
mydict["c"] = c
print mydict

{'a': {'a': 'I am a'}, 'c': {'c': 'I am c'}, 'b': {'b': 'I am b'}}
Denim answered 19/2, 2010 at 20:46 Comment(0)
B
0

Maybe one nested dictionary is okay... but even then you're asking to get confused later and there's a pretty good chance that if you're already doing nested stuff you're gonna want more information in it later.

In general I'd say take a step back and see if all that information in the dictionaries is needed. Try to simplify it first. If it really is all needed, I'd try to make a simple class for it. Might be a little overkill but like I said, if you're going down that road already, you're probably gonna end up adding more information later. It's easier to modify a class than it is to try and figure out all that nested information and make your code work with it later.

Bivouac answered 19/2, 2010 at 20:57 Comment(0)
D
0

You can also use a struct type. Like Dictionary<string, Custom> dict. The struct type is like a dict I guess, but its more rigid.

Dalessio answered 17/3, 2023 at 14:50 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Depurative

© 2022 - 2024 — McMap. All rights reserved.