views:

272

answers:

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!)?

+6  A: 

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 :)

truppo
+1 for citing the Zen
Triptych
+1 for "it depends on what you are doing"
MatrixFrog
+5  A: 

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.

Mike Graham
Tuples also have significantly less overhead than dictionaries.
Wallacoloo
Thanks, can I get an example?
Vishal
Example: `d[(a, b, c)] = x`
Max Shawabkeh
@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}`.
Mike Graham
@Max S., When indexing, the parens there are optional and would usually be omitted.
Mike Graham
@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.
Max Shawabkeh
@Mike: What's the difference if I am using string "a-b-c" as a key instead of (a, b, c)?
Vishal
@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].
Mike Graham
@Mike: I got it, thanks
Vishal
+1  A: 

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.

Geo
A: 

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'}}
Michael Dillon
A: 

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.

The Jug