I have a dictionary of an undefined size (as big as the user wants) but I want to be able to delete items in the dictionary if needed, my question is how to I move all of the dictionary keys once one has been removed, i.e
transDict={0:"Charlie", 1:"Alan", 2:"Jake", 3:"Rose"}
delkey = raw_input('What key to delete')
transDict[delkey] = transDict.pop[int(delkey + 1)]
# this is the bit isn't working code just an
# example of the functionality I need
for _id in transDict.keys(int(delkey) + 1:):
transDict[_id] = transDict[(int(_id) + 1)]
but then I need to shift all other elements from 3 onwards down a step, the problem I have is the dictionary can be 5 or 50 elements big.
Thanks
For this you should be using a list, not a dictionary, because a list is a structure which natively maps numerical indices to elements. When you remove an element from a list using del transList[delkey]
, it automatically shifts remaining elements in the list down to maintain consecutive indices.
You should definitely be using a list
for such a feature, which would handle that feature per design.
>>> for i in l:
... print(l.index(i), i)
...
(0, 'Charlie')
(1, 'Alan')
(2, 'Jake')
(3, 'Rose')
>>> del l[2]
>>> for i in l:
... print(l.index(i), i)
...
(0, 'Charlie')
(1, 'Alan')
(2, 'Rose')
But for the sake of answering your question here's a solution:
>>> def remove_key(d, del_key):
... new_dict = {}
... for key, val in d.items():
... if key < del_key:
... new_dict[key] = val
... elif key > del_key:
... new_dict[key-1] = val
... else: # key == del_key
... continue
... return new_dict
...
>>> transDict={0:"Charlie", 1:"Alan", 2:"Jake", 3:"Rose"}
>>> remove_key(transDict, 2)
{0: 'Charlie', 1: 'Alan', 2: 'Rose'}
What was wrong in your algorithm:
for _id in transDict.keys(int(delkey) + 1:):
transDict[_id] = transDict[(int(_id) + 1)]
it is that,
keys()
method. The right syntax is to use the []
operator, e.g. .keys()[2:]
,delkey+1
of your dictionary, discarding the two possibilities, and you're shifting all the following values by one:
dict
's definition),So to build the algorithm I suggested you, I'm building a new dictionary, and while copying keys from the first dictionary to the new one, I consider the three following cases:
Then you can assign that new dictionary to the old one.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With