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"]
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With