Skip to content

Instantly share code, notes, and snippets.

@meadhikari
Created November 29, 2012 18:16
Show Gist options
  • Save meadhikari/4170888 to your computer and use it in GitHub Desktop.
Save meadhikari/4170888 to your computer and use it in GitHub Desktop.
Reduce the list and add to why on every deletion
my_input = [{'why': [u'The Matrix Revolutions'], 'title': u'The Matrix'},
{'why': [u'The Matrix Revolutions'], 'title': u'Inception'},
{'why': [u'The Matrix'], 'title': u'The Matrix Revolutions'},
{'why': [u'The Matrix'], 'title': u'Inception'},
{'why': [u'Saving Private Ryan'], 'title': u'The Hurt Locker'},
{'why': [u'Black Hawk Down'], 'title': u'Saving Private Ryan'},
{'why': [u'Black Hawk Down'], 'title': u'The Kingdom'},
{'why': [u'Black Hawk Down'], 'title': u'The Hurt Locker'},
{'why': [u'A Walk To Remember'], 'title': u'The Notebook'},
{'why': [u'Cast Away'], 'title': u'Forrest Gump'},
{'why': [u'Forrest Gump'], 'title': u'The Notebook'},
{'why': [u'Body of Lies'], 'title': u'The Kingdom'},
{'why': [u'Unthinkable'], 'title': u'Syriana'},
{'why': [u'Lord of War'], 'title': u'Syriana'},
{'why': [u'The Terminal'], 'title': u'Forrest Gump'},
{'why': [u'Titanic'], 'title': u'The Notebook'},
{'why': [u'Catch Me If You Can'], 'title': u'Forrest Gump'},
{'why': [u'Syriana'], 'title': u'The Constant Gardener'},
{'why': [u'Nothing But the Truth'], 'title': u'Syriana'},
{'why': [u'Hotel Rwanda'], 'title': u'The Constant Gardener'},
{'why': [u'Hotel Rwanda'], 'title': u'Syriana'},
{'why': [u'Inception'], 'title': u'The Matrix'},
{'why': [u'Inception'], 'title': u'The Matrix Revolutions'},
{'why': [u'The Hurt Locker'], 'title': u'Saving Private Ryan'},
{'why': [u'The Hurt Locker'], 'title': u'The Kingdom'}]
#remove all the duplicate 'title' and append each 'why'
my_output = [{'why': [u'Unthinkable',u'Lord of War',u'Hotel Rwanda'], 'title': u'Syriana'}]
@bikeshedder
Copy link

How about the following piece of code?

output = {}
for d in my_input:
    whys = output.setdefault(d['title'], [])
    whys.append(d['title'])

@rmartinjak
Copy link

looks ugly, but does the job ;)

a = groupby(
    sorted(
        [(x['title'], x['why'][0]) for x in my_input]),
    key=lambda x: x[0])
my_output = [{'title': x[0], 'why': [y[1] for y in x[1]]} for x in a]

@rmartinjak
Copy link

oh, i forgot that you need to

from itertools import groupby

@bikeshedder
Copy link

output = {}
for d in my_input:
    whys = output.setdefault(d['title'], [])
    whys.extend(d['why'])

@bikeshedder
Copy link

And for the complete example which produces the correct output I'd go for:

output = {}
for d in my_input:
    try:
        unique = output[d['title']]
    except KeyError:
        unique = output[d['title']] = {'why': [], 'title': d['title']}
    unique['why'].extend(d['why'])
output = output.values()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment