1

Please note, similar question to this particular one is Not present here already. But different ones present. So posting the question for suggestions.

Its for removing the Duplicates from Individual List item from the Nested List and preserving the order.

Please see below question for more details and suggest.

list_1 = [['A1', 'B1', 'A1'],
          ['A2', 'B2', 'B2'],
          ['A3', 'B3', 'C3']]

First item in list_1 has 2 'A1's and Second item has 2 'B2's and no dupes in Third item. So need to eliminate the dupes from First & Second items and also order needs to be preserved.

Expected output list as below:

list_op = [['A1', 'B1'],
           ['A2', 'B2'],
           ['A3', 'B3', 'C3']]

Note: Below solution is removing the Dupes as required from Nested List, but Not preserving the order within Individual List items, after dupes removal.

[list(set(sub_list)) for sub_list in list_1]
mlds522
  • 55
  • 5
  • 2
    Possible duplicate, https://stackoverflow.com/a/480227/4985099 – sushanth Jul 06 '20 at 12:42
  • using `map()`: # Data: `list_1 = [['A1', 'B1', 'A1'], ['A2', 'B2', 'B2'], ['A3', 'B3', 'C3']]` # Remove duplicate list elements: `[*map(lambda x: [*sorted(set(x), key=x.index)], list_1)]` – hello_friend Jul 06 '20 at 12:49
  • 1
    From the dupe [answer](https://stackoverflow.com/a/39835527/7505395): `l2 = [list(dict.fromkeys(inner)) for inner in list_1]` for python 3.6+ where dict keys insert order is kept – Patrick Artner Jul 06 '20 at 12:50

2 Answers2

0
list_1 = [['A1', 'B1', 'A1'],
          ['A2', 'B2', 'B2'],
          ['A3', 'B3', 'C3']]

list2 = []
for l in list_1:
    tmp = []
    for i in l:
        if i not in tmp:
            tmp.append(i)

    list2.append(tmp)
print(list2)
0

You can try list comprehension that checks in the inner list if the iterated value is equal to the return index from the index() method that will print only the first item of the duplicates items.

list_op = [[y for inx, y in enumerate(i) if inx == i.index(y)] for i in list_1]
print(list_op)

Output

[['A1', 'B1'], 
 ['A2', 'B2'], 
 ['A3', 'B3', 'C3']]
Leo Arad
  • 4,452
  • 2
  • 6
  • 17