-1

I have a list that containts a tuple, and that tuple contains another list with values:

list
[('5c3b542e2fb59f2ab86aa81d', 
['hello', 'this', 'is', 'a', 'test', 'sample', 'this', 'is', 'duplicate'])]

As you can notice 'this and 'is' are already present in this list and I would like to remove them.

So I want something like this:

new_list
[('5c3b542e2fb59f2ab86aa81d', 
['hello', 'this', 'is', 'a', 'test', 'sample', 'duplicate'])]

Just to be clear, the List above contains multiple values, see my screenshot attached to this question.screenshot of list format

So far I tried the following method:

final_list = []

for post in new_items:
    if post[1] not in final_list:
       _id = post[0]
       text = post[1]
       together = _id, text
       final_list.append(together)

I tried to loop through each item in the list and if it's not in the list, it would be added to the final_list. But so far this method doesn't work and gives me the exact same list back.

Solaiman
  • 298
  • 2
  • 7
  • 22

1 Answers1

1

One easy way to remove duplicates from a list is to first convert it to a set (sets cannot hold duplicate elements) and the convert it back to a list. Example

alist = ['a', 'b', 'a']

alist = list(set(alist))

The result of alist will be ['a', 'b']

You can add this in your for loop

mbass
  • 95
  • 2
  • 10
  • Thanks for your answer. I solved it yesterday with the answer of Alexander in https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order – Solaiman Jan 18 '19 at 11:50