eeyyyyyy fellow python spice enjoyers, what would you say is more "readable" / straight-forward for a nested dictionary creation/update scenario?

a = {} b = "pumpkin" c = "spice" if b in a: a[b][c] = True else: a[b] = {c: True}vsa = {} b = "python" c = "spice up ya life" a.update({b: {c: True}})
the second one is definitely less code but I feel like it's... a tich too clever. Or do people often use that second approach? I've classically stuck to the first one but.

Assume it will be read by non-python experts, as well, but that visually things are getting a little cluttered so maybe condensing some things would be nice...

#python #pythonQuestions

@aud can there be keys other than "spice" in a["pumpkin"]? if not you could just write a["pumpkin"] = {"spice": True}. in general i think using the key names over a and b here is already a win unless they're really long or something

no idea if it works for your use case but i'd like to write something like this with defaultdicts. like:

a = collections.defaultdict(dict)
a["pumpkin"] = {"spice": True}

or something, but idk how much of an impact that would have on surrounding code

@monorail oh, they're loop variables, in this case, which is why I defaulted to using b and c in the example, I suppose.

@aud ohh yeah that makes sense

i really like the setdefault answer elsewhere in the thread, you should ignore me haha