Dec-09-2020, 09:50 AM
One way is to use
setdefault
.>>> test = dict() >>> test.setdefault('key1', list()).append(2) >>> test {'key1': [2]}It could be used in for-loops etc:
>>> test = dict() >>> nums = [2, 3, 7, 19, 24] >>> for num in nums: ... if num % 2: ... test.setdefault(num, list()).append(num**2) ... >>> test {3: [9], 7: [49], 19: [361]}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.