1

I have a list

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]

and I want to make my list to

A = [['1'],['1','2'],['1','2','3'],['3']]

ie I want to remove duplicate elements within the elements in a list ..

cs95
  • 379,657
  • 97
  • 704
  • 746
Prayag Bhatia
  • 366
  • 3
  • 16
  • 3
    If order doesn't matter just convert to `set` and back to `list`: `[list(set(x)) for x in A]` – AChampion Aug 26 '17 at 17:00
  • Possible duplicate of [Removing duplicates in lists](https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists) – wwii Aug 26 '17 at 21:04

3 Answers3

3

One-liner (If order doesn't matter) :

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
A = [list(set(a)) for a in A]
print(A) # => [['1'], ['2', '1'], ['3', '2', '1'], ['3']]

One-liner (If order matters) :

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
A = [sorted(set(a), key=a.index) for a in A]
print(A) # => [['1'], ['1', '2'], ['1', '2', '3'], ['3']]
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1

A functional version, with functools:

>>> import functools
>>> A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
>>> print ([functools.reduce(lambda result,x:result if x in result else result+[x], xs, []) for xs in A])
[['1'], ['1', '2'], ['1', '2', '3'], ['3']]

The lambda function adds an element to the result list only if that element is not present in the list. Not very efficient, but keeps the order of elements.

Also note that with Python 2, you don't need to import functools: reduce is a builtin function.

jferard
  • 7,835
  • 2
  • 22
  • 35
0

You can use a generator:

def remove_dups(l):
   for a in l:
      new_l = []
      for b in a:
          if b not in new_l:
             new_l.append(b)
      yield new_l

A = [['1'],['1','2'],['1','2','3','1','2'],['3','3','3']]
print(list(remove_dups(A)))

Output:

[['1'], ['1', '2'], ['1', '2', '3'], ['3']]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102