How to change key in python dictionary

How to change key in python dictionary:
for example:

data={'998905653388.0':('1254', '1255', 'Hello world'), =>
      '998905653388':('1254', '1255', 'Hello world')}

I tried like this:

for key in data.keys():
    data[key] = data[new_key[0]]
    data.pop(key, None)

But it throws an error:

TypeError: unhashable type: ‘list’

Or you can suggest other options.
Thank you.

>Solution :

You could iterate trought the list of your keys so it create a copy of them and once you modify keys inside your dict, it won’t conflict.

For each iteration you create a new key:val in your dict and pop out the old key

for key in list(data.keys()): data[key.split('.')[0]] = data.pop(key)

Leave a Reply