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

(I probably should have used the same keys in both examples, in hindsight, but I'd already removed a Dune reference for readability so I wasn't gonna lose another reference)
@aud seems like a good case for either a defaultdict (if appropriate) or maybe dict.setdefault() (which confusingly acts like a GETTER but has the side effect of assigning first if necessary)
@SnoopJ trying to look up defaultdict and yeaaaaaaah I typo'ed that one right away...

Actually, looking it up, yeah, that might be perfect!

@SnoopJ YEP, nifty nifty!

from collections import defaultdict a = defaultdict(dict) b = "spice" c = "must flow" a[b][c] = True
works!

@SnoopJ but in case that's too fancypants, you can always just go with

a = defaultdict() b = "spice" c = "must flow" a[b] = {c: True}

@aud that doesn't account for when the key is already there though, right? Could just as well be a dict if not worried about that

defaultdict can also get weird when values are heterogeneous which is one more reason not to do heterogeneous values 

@SnoopJ ah, you're right!

and nope, full ass homo (geneous) here
@SnoopJ @aud yes 100% absolutely this 😁
@SnoopJ @aud ohhhh i didn't think of setdefault, good answer

@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

@aud Beware that those are not equivalent. The first one will keep the other keys in 'b' if there are any, the second will update 'b' to be a dictionary with a single 'c' key.

So it all depends what you want to do.

@nicoe ohhhhh, you're right. Hadn't even considered that angle.

And in fact, I think that IS relevant here. Good catch, thank you!
@nicoe Yeah; not only is defaultdict() the cleaner option, readability wise... but it also maintains the behavior in example A without writing over it ala example B.

Part of the issue is these are loop variables running over an opened file whose format I'm only haltingly familiar with, so I had totally missed that yes, in fact, it's potentially possible the nested dictionaries need to have more than one value...