How do I check if there are duplicates in a flat list?

teggy picture teggy · Oct 9, 2009 · Viewed 248.1k times · Source

For example, given the list ['one', 'two', 'one'], the algorithm should return True, whereas given ['one', 'two', 'three'] it should return False.

Answer

Denis Otkidach picture Denis Otkidach · Oct 9, 2009

Use set() to remove duplicates if all values are hashable:

>>> your_list = ['one', 'two', 'one']
>>> len(your_list) != len(set(your_list))
True