Skip to content

Instantly share code, notes, and snippets.

@ksysctl
Created August 10, 2013 18:28
Show Gist options
  • Select an option

  • Save ksysctl/6201577 to your computer and use it in GitHub Desktop.

Select an option

Save ksysctl/6201577 to your computer and use it in GitHub Desktop.
Change dictionary keys in a list
# 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