V
V
Valery Semenov2012-11-09 18:30:46
Python
Valery Semenov, 2012-11-09 18:30:46

Is there a more canonical and beautiful way to get the list?

let
icons = [{event_id:1, x:1},{event_id:2, x:2},{event_id:1, x:3}]
how to get a list of icons with unique event_ids. i.e. [{event_id:1, x:1},{event_id:2, x:2}]
my version:

tmp = set()
unic = set()
for icon in icons:
     if not icon['event_id'] in tmp:
         unic.add(icon)
         tmp.add(icon['event_id'])

knowing that python can provide beautiful solutions, I assume that here you can solve the problem easier (or more beautiful)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
leventov, 2012-11-09
@Ravall

unic = {icon['event_id']: icon for icon in reversed(icons)}.values()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question