Created
January 16, 2013 19:06
-
-
Save cshoe/4549823 to your computer and use it in GitHub Desktop.
This file contains 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
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