BlandCorporation
BlandCorporation

Reputation: 1344

How can one get the indices of duplicate elements in a list of OrderedDicts?

I have a list of OrderedDicts and I want to get the indices in that list of the elements that are duplicates. Conceptually, it is a bit like the following example, which features a list of ints:

>>> def indices_of_list_element_duplicates(x):
...     seen = set()
...     for index, element in enumerate(x):
...         if isinstance(element, list):
...             element = tuple(element)
...         if element not in seen:
...             seen.add(element)
...         else:
...             yield index
... 
>>> a = [1, 2, 3, 4, 5, 6, 1, 1, 9, 1]
>>> indices = [index for index in indices_of_list_element_duplicates(a)]
>>> indices
[6, 7, 9]

How can the equivalent of this be done for a list of OrderedDicts? When I try to this function on the OrderedDicts, I encounter the following error:

TypeError: unhashable type: 'OrderedDict'

Upvotes: 0

Views: 24

Answers (1)

Michael Hoff
Michael Hoff

Reputation: 6338

from collections import OrderedDict
# ...
if isinstance(element, OrderedDict):  # checking for type dict would be enough
    element = tuple(element.items())
# ...

This converts the dictionary to a tuple of tuples which can, in turn, be an element of your set. Before, you were trying to add an object to the set which does not implement hashing.

Be aware that the given dictionaries have to be recursively restricted to hashable value types. Otherwise you will experience a similar issue.

from collections import OrderedDict
d = OrderedDict(a=[1,2,3])
set().add(tuple(d.items()))
TypeError: unhashable type: 'list'

Upvotes: 1

Related Questions