How do I check if there are duplicates in a flat list?
问题 For example, given the list ['one', 'two', 'one'] , the algorithm should return True , whereas given ['one', 'two', 'three'] it should return False . 回答1: Use set() to remove duplicates if all values are hashable : >>> your_list = ['one', 'two', 'one'] >>> len(your_list) != len(set(your_list)) True 回答2: Recommended for short lists only: any(thelist.count(x) > 1 for x in thelist) Do not use on a long list -- it can take time proportional to the square of the number of items in the list! For