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