Created
August 10, 2013 18:28
-
-
Save ksysctl/6201577 to your computer and use it in GitHub Desktop.
Change dictionary keys in a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Original list of dicts | |
| old = [{ | |
| 'product_name': 'A Name', | |
| 'product_id': '666' | |
| }] | |
| # New keys | |
| mapping = { | |
| 'product_name': 'name', | |
| 'product_id': 'id' | |
| } | |
| new = [dict((mapping[k], v) for k,v in o.iteritems()) for o in old] | |
| ''' | |
| Now it looks like | |
| [{ | |
| 'name': 'A Name', | |
| 'id': '666' | |
| }] | |
| ''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment