0

I have a 2d list with more than 20 rows but Im having a bit of trouble trying to figure out how to remove duplicates from each sublist in a list without changing the remaining order of unique items in each sub list. Example below,

list1 = [['a', 'a', 'b', 'b', 'b', 'v', 'v', 'v', 'v', 'x', 'x', 'p'], 
        ['b', 'c', 'd', 'c'], ['a', 'j', 'j', 'j', 'c', 'c', 'f', 'f', 'h', 'h', 'h', 'h']]

I would like the output to be like this:

Output = [['a', 'b', 'v', 'x', 'p'], ['b', 'c', 'd'], ['a', 'j', 'c', 'f', 'h']]
outkast20
  • 71
  • 7

3 Answers3

1

Counter can help without changing the remaining order.

from collections import Counter
list(map(list,map(Counter, list1)))
[['a', 'b', 'v', 'x', 'p'], ['b', 'c', 'd'], ['a', 'j', 'c', 'f', 'h']]
1

set() can help.

output = [list(set(l)) for l in list1]

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
holly
  • 34
  • 3
0

What is the type of each element? If it is an immutable data type like a int, str, tuple.. you can write like this

 [list(set(i)) for i in list1]
Xerxes
  • 1