rename keys in a dictionary
Posted
by user366660
on Stack Overflow
See other posts from Stack Overflow
or by user366660
Published on 2010-06-14T20:35:57Z
Indexed on
2010/06/14
20:42 UTC
Read the original article
Hit count: 195
python
|dictionary
i want to rename the keys of a dictionary are which are ints, and i need them to be ints with leading zeros's so that they sort correctly.
for example my keys are like:
'1','101','11'
and i need them to be:
'001','101','011'
this is what im doing now, but i know there is a better way
tmpDict = {}
for oldKey in aDict:
tmpDict['%04d'%int(oldKey)] = aDict[oldKey]
newDict = tmpDict
© Stack Overflow or respective owner