Z Xie Z Xie - 3 months ago 18
Python Question

Reset new keys to a dictionary

I have a python dictionary.

A=[0:'dog',1:'cat',3:'fly',4,'fish',6:'lizard']


I want to reset the keys according to
range(len(A))
(the natural increment), which should look like:

new_A=[0:'dog',1:'cat',2:'fly',3:'fish',4:'lizard']


How could I do that?

BPL BPL
Answer

Here's a working example for both py2.x and py3.x:

A = {0: 'dog', 1: 'cat', 3: 'fly', 4: 'fish', 6: 'lizard'}

B = {i: v for i, v in enumerate(A.values())}
print(B)