Remove duplicate tuples with included lists from the list

I have a list with tuples:

managed_list = [ ('a', [1,2]), ('a', [1,2]), ('b', [2,2]), ('b', [2,2])]

      

you need to get:

managed_list = [ ('a', [1,2]), ('b', [2,2]) ]

      

tried:

seen = set()
[[n for n in x if n not in seen and not seen.add(n)] for x in managed_list]

      

I get:

TypeError: unhashable type: 'list'

      

+3


source to share


2 answers


That's right, you cannot use list

either a structure containing list

(or other non-splicing type) in set

. Without changing the structure of the input, you can use itertools.groupby

and then simply discard the iterator through duplicates:

import itertools
uniques = [x[0] for x in itertools.groupby(sorted(managed_list))]

      

By the way, if it weren't for the split key problem (for example, if there were tuples instead of lists), your expression could be simplified to:



list(set(managed_list))

      

You don't need any additional list comprehension code for this.

+3


source


You can also use collections.OrderedDict

to remove duplicate keys.

>>> from collections import OrderedDict
>>> OrderedDict([ ('a', [1,2]), ('a', [1,2]), ('b', [2,2]), ('b', [2,2])]).items()
[ ('a', [1,2]), ('b', [2,2]) ]

      



Be aware that in the case of duplicate keys, the rightmost entry will be the one included in the output.

+1


source







All Articles