Reputation: 1
I want to extend an existing key in a dict with an information.
The key 'abc'
should then be completed with a 'd'
so that the result is 'abcd'
.
Something like this:
info1='abc'
info2='d'
DataDict[info1 '%i' %info2] = DataDict.pop(info1)
but it does not work...
Upvotes: 0
Views: 206
Reputation: 77837
For a dict, this is a contradiction: a different key is entirely a different entry. You can, perhaps, make it a little more readable for your algorithm, but you still have to create a new key and remove the old one.
old_key = "abc"
extension = 'd'
my_dict[old_key + extension] = my_dict[old_key]
del my_dict[old_key]
Depending on your use case, you may want to make a new copy of the old value.
Ah ... I see tha tyou are comfortable with pop
. In that case, you were quite close with your (edited) solution:
my_dict[old_key + extension] = my_dict.pop(old_key)
Upvotes: 1