I have an empty dictionary. Name: dict_x
It is to have keys of which values are lists.
From a separate iteration, I obtain a key (ex: key_123
), and an item (a tuple) to place in the list of dict_x
's value key_123
.
If this key already exists, I want to append this item.
If this key does not exist, I want to create it with an empty list and then append to it or just create it with a tuple in it.
In future when again this key comes up, since it exists, I want the value to be appended again.
My code consists of this:
Get key and value.
See if NOT key exists in dict_x
.
and if not create it: dict_x[key] == []
Afterwards: dict_x[key].append(value)
Is this the way to do it? Shall I try to use try/except
blocks?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…