In [1]:
def has_duplicates(seq):
return len(seq) != len(set(seq))
In [2]:
l = [0, 1, 2]
print(has_duplicates(l))
In [3]:
l = [0, 1, 1, 2]
print(has_duplicates(l))
In [4]:
l_2d = [[0, 1], [1, 1], [0, 1], [1, 0]]
# print(has_duplicates(l_2d))
# TypeError: unhashable type: 'list'
In [5]:
def has_duplicates2(seq):
seen = []
unique_list = [x for x in seq if x not in seen and not seen.append(x)]
return len(seq) != len(unique_list)
In [6]:
l_2d = [[0, 0], [0, 1], [1, 1], [1, 0]]
print(has_duplicates2(l_2d))
In [7]:
l_2d = [[0, 0], [0, 1], [1, 1], [1, 1]]
print(has_duplicates2(l_2d))
In [8]:
l = [0, 1, 2]
print(has_duplicates2(l))
In [9]:
l = [0, 1, 1, 2]
print(has_duplicates2(l))
In [10]:
l_2d = [[0, 1], [2, 3]]
print(sum(l_2d, []))
In [11]:
print(has_duplicates(sum(l_2d, [])))
In [12]:
l_2d = [[0, 1], [2, 0]]
print(has_duplicates(sum(l_2d, [])))