In Python, is there a way to check for duplicate items in a list, and if there is, then remove them? I’m looking for something like this:
>>> def check():
>>> # put code here
>>> list = ["foo", "foo", "bar"]
>>> check(list)
>>> list
["foo", "bar"]
>>> list2 = ["foo", "bar", "example"]
>>> check(list2)
>>> list2
["foo", "bar", "example"]
Thanks in advance!
Update:
Guys, I’m really new at programming, and the order doesn’t really matter. So a for loop should be fine. But thanks anyways!
A
setmight be a better data structure here, as it can’t have duplicates in the first place. You can also use it as a tool to uniquify your list:Or if you need to preserve the order, you can keep your data in an
OrderedDict:Note that for performance reasons it would be more ideal to use the right data structure in the first place, rather than using a raw list and removing duplicates from it after every operation using one of these transformations.