Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

rename keys in dictionary python

# For a regular dict
mydict[k_new] = mydict.pop(k_old)

# To preserve ordering
d = {0:0, 1:1, 2:2, 3:3}
{"two" if k == 2 else k:v for k,v in d.items()}
# => {0: 0, 1: 1, 'two': 2, 3: 3}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #rename #keys #dictionary #python
ADD COMMENT
Topic
Name
8+3 =