1

I have a lists with several elements and i'd like to remove the duplicated, here's an example of a list:

list = [{'status': 'OK', 'mid': '6639'}, {'status': 'OK', 'mid': '6639'}, {'status': 'OK', 'mid': '6640'}, {'status': 'OK', 'mid': '6640'}, {'status': 'OK', 'mid': '6641'}, {'status': 'OK', 'mid': '6641'}, {'status': 'OK', 'mid': '6642'}, {'status': 'OK', 'mid': '6643'}]

I have tried to use the function set but i have an error it says that the list is unhashable, does someone have an idea of how to remove the duplications ?

Kirk ERW
  • 167
  • 1
  • 2
  • 14
  • 3
    You could make those dictionaries hashable: http://stackoverflow.com/questions/1151658/python-hashable-dicts – poke Apr 23 '13 at 15:31
  • 2
    See this question: http://stackoverflow.com/questions/9427163/remove-duplicate-dict-in-list-in-python – Nicolas Apr 23 '13 at 15:31

1 Answers1

4

You can still use a set() but you need to turn each list into a tuple; here we use a sorted tuple of the keys and values to find duplicates accurately:

set(tuple(sorted(el.items())) for el in somelist)

To keep the list in order and keep your dictionaries intact, you could use:

seen = set()
seen_add = seen.add
[x for x in somelist if tuple(sorted(x.items())) not in seen and not seen_add(tuple(sorted(x.items())))]

Demo for the latter:

>>> seen = set()
>>> seen_add = seen.add
>>> [x for x in somelist if tuple(sorted(x.items())) not in seen and not seen_add(tuple(sorted(x.items())))]
[{'status': 'OK', 'mid': '6639'}, {'status': 'OK', 'mid': '6640'}, {'status': 'OK', 'mid': '6641'}, {'status': 'OK', 'mid': '6642'}, {'status': 'OK', 'mid': '6643'}]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343