-4

I want to remove duplicates from each list in a list of lists. Is there an efficient, pythonic way to do that?

v=[[[234,354,400,400,230,300,500]],[[2,2,24]],[[56,102,98]]]

And I'd liked to remove all the duplicates from each individual list, then sort each individual list, producing an output of:

[[[230,234,300,354,400,500]],[[2,24]],[[56,98,102]]]

If at all possible, I'd like to keep the structure of the lists as they are(double brackets). Is there any (relatively) easy way to do this in python?

I've tried a few other ways, but found it difficult to work with the structure of the list and the fact that it is unhashable makes it a little tougher.

cs4
  • 21
  • 2
  • http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order?rq=1 – Marc B Aug 19 '15 at 21:17
  • Or the one Marc suggested. Essentially this is a duplicate because it's exactly the same as deduping a regular list, but you'll end up doing it with a loop of some kind. – Two-Bit Alchemist Aug 19 '15 at 21:18

1 Answers1

0

Maybe something like:

In [21]: vv=[[234,354,400,400,230,300,500],[2,2,24],[56,102,98]]

In [22]: [sorted(list(set(v))) for v in vv]
Out[22]: [[354, 230, 234, 300, 400, 500], [24, 2], [56, 98, 102]]

By the way, your data structure there is a bit weird. It has mismatched brackets and each second layer list is wrapped in an extra list, I've assumed for the moment that these are typos and not intentional.

BWStearns
  • 2,567
  • 2
  • 19
  • 33