Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

renaming key in every dictionary in list, python

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading