0

Trying to remove duplicates in list of list and storing it.

Original List:

list1 = [[a, b, a, b], [b, c, d, c], [a, c, c]]

Looking for output:

list1 = [[a, b], [b,c,d], [a,c]]

My code:

unique_list = []
for sublist in list1:
 element = [elem for elem in sublist if elem not in unique_list]
 if len(element):
 unique_testset.append(element)

My code appends a sublist multiple times and doesn't get rid of the duplicates.

asked Sep 18, 2020 at 10:38
4

4 Answers 4

3

Removing duplicates the simple way

A classic, efficient way to remove duplicates from a list in python is to build a set from the list: removing duplicates in lists

list_with_dups = [1, 1, 2, 3, 2]
list_without_dups = list(set(list_with_dups))

You can apply this method repeatedly using a list comprehension:

list1 = [['a', 'b', 'a', 'b'], ['b', 'c', 'd', 'c'], ['a', 'c', 'c']]
without_duplicates = [list(set(sublist)) for sublist in list1]
# = [['b', 'a'], ['d', 'b', 'c'], ['c', 'a']]

Removing duplicates whilst conserving order

Applying How do you remove duplicates whilst conserving order? to a list of lists:

def f7(seq):
 seen = set()
 seen_add = seen.add
 return [x for x in seq if not (x in seen or seen_add(x))]
list1 = [['a', 'b', 'a', 'b'], ['b', 'c', 'd', 'c'], ['a', 'c', 'c']]
without_duplicates = [f7(sublist) for sublist in list1]
# = [['a', 'b'], ['b', 'c', 'd'], ['a', 'c']]
answered Sep 18, 2020 at 10:47
5
  • Dear @MuhammadRizwanMunawar, thank you for trying to improve my answer by suggesting an edit. Your edit suggestion to my answer is actually just deleting parts of my answer rather than improving it. This is doubly annoying because: 1) I cannot reject your edit. 2) I wanted to edit my answer to make it clearer, but I cannot, because your suggested edit prevents me from editing further. Please revert your edit. I do not understand why you decided to delete parts of my answer and this is very annoying. Commented Sep 18, 2020 at 10:54
  • Thanks! This works for me but it changes the order of the sublist. Is there any way to preserve the order? Commented Sep 18, 2020 at 10:54
  • @Miztory Yes, set structures in python do not conserve order. Here is a stackoverflow question about removing duplicates whilst conserving order: stackoverflow.com/questions/480214/… Commented Sep 18, 2020 at 10:57
  • @Stef I think I'll be able to continue with my work even with a different order.Thanks for the help. :) Commented Sep 18, 2020 at 11:13
  • @Miztory I've updated the answer to conserve order. Commented Oct 1, 2020 at 10:28
0

try this one...

 list1 = [[a, b, a, b], [b, c, d, c], [a, c, c]]
 
 import itertools
 list1.sort()
 list(list1 for list1,_ in itertools.groupby(list1))
answered Sep 18, 2020 at 10:45
0

One way you can achieve this is using:

list1 = [[a, b, a, b], [b, c, d, c], [a, c, c]]

for sublist in list1:
 l = list(set(sublist))
 if l:
 unique_list.append(l)

set removes all the duplicate values and we have unique values

answered Sep 18, 2020 at 10:45
2
  • try to implement with respect to question not in jorunal. Thanks Commented Sep 18, 2020 at 10:47
  • 1
    if len will always evaluate to True. You could just leave it (even if you use it correctly). Commented Sep 18, 2020 at 11:06
0

Use This code it'll help you

 import numpy as np
 lis=[['a','b','a'], ['b','c','d'], ['c','a','c']]
 uni=np.unique(lis)
 print(uni)
answered Sep 18, 2020 at 12:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.