Home > Back-end >  renaming key in every dictionary in list, python
renaming key in every dictionary in list, python

Time:07-11

I can't figure out how to rename "Number" to "ProjectId" in every dictionary in the list of dictionaries. Can anyone help? I tried it in the renaming() function but it doesn't work.

pojects = 
[{'Number': '5',
  'Name': 'CFO'},
 {'Number': '7',
  'Name': 'Head of Product'}, 
 {'Number': '6',
  'Name': 'CEO'}]
def renaming(projects):
    for i in projects:
        i['ProjectId'] = i.pop('Number')
        return projects

CodePudding user response:

you can add a new key named 'ProjectId' that takes the value of 'Number' and then delete number

for item in projects:
    item['ProjectId'] = item['Number']
    del item['Number']
  • Related