Skip to content

Instantly share code, notes, and snippets.

@cshoe
Created January 16, 2013 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cshoe/4549823 to your computer and use it in GitHub Desktop.
Save cshoe/4549823 to your computer and use it in GitHub Desktop.
def main():
# this is the dict that we want to trim down.
# basically we want to collapse `foo` into a list for a common `id`
bloated_map = [{'foo': 2, 'id': 160},
{'foo': 314, 'id': 160},
{'foo': 314, 'id': 161},
{'foo': 2, 'id': 161}]
# this gives us a list of unique ids
id_list = set([x['id'] for x in bloated_map])
# this is going to hold our ouput
slim_map = []
# loop through unique ids
for i in id_list:
# make a list of foos associated with id `i`
foos = [x['foo'] for x in bloated_map if x['id'] == i]
slim_map.append({'id': i, 'foos': foos})
print slim_map
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment