Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how do you remove duplicates from one or multiple lists? [duplicate]

For example, if I had:

a = ["apples", "bananas", "cucumbers", "bananas"]

How could I remove the duplicate "bananas" so that:

a = ["apples", "bananas", "cucumbers"]

Also, if I had:

a = ["apples", "bananas", "cucumbers"]

b = ["pears", "apples", "watermelons"]

How could I remove the duplicate "apples" from both lists so that:

a = ["bananas", "cucumbers"]

b = ["pears", "watermelons"]
like image 577
HashtagAbuse Avatar asked Aug 11 '26 20:08

HashtagAbuse


2 Answers

The set-based solutions don't retain the order of the items. The following will keep the items in order and delete all but the first occurrence of each, using an auxilary set to keep track of which items have already been seen.

seen = set()
a = [seen.add(item) or item for item in a if item not in seen]

If you want to reuse the same list object, you can do that this way:

seen = set()
a[:] = (seen.add(item) or item for item in a if item not in seen)
like image 126
kindall Avatar answered Aug 13 '26 12:08

kindall


Use built-in functions set

a = ["apples", "bananas", "cucumbers", "bananas"]
a = list(set(a))
print(a)

In second case, use list comprehension

a = ["apples", "bananas", "cucumbers"]
b = ["pears", "apples", "watermelons"]

r = [i for i in a if i not in b] + [i for i in b if i not in a]    
print(r)
like image 23
macabeus Avatar answered Aug 13 '26 12:08

macabeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!