In [1]:
l = [3, 3, 2, 1, 5, 1, 4, 2, 3]
In [2]:
print(set(l))
In [3]:
print(list(set(l)))
In [4]:
print(dict.fromkeys(l))
In [5]:
print(list(dict.fromkeys(l)))
In [6]:
print(sorted(set(l), key=l.index))
In [7]:
l_2d = [[1, 1], [0, 1], [0, 1], [0, 0], [1, 0], [1, 1], [1, 1]]
In [8]:
# l_2d_unique = list(set(l_2d))
# TypeError: unhashable type: 'list'
In [9]:
# l_2d_unique_order = dict.fromkeys(l_2d)
# TypeError: unhashable type: 'list'
In [10]:
def get_unique_list(seq):
seen = []
return [x for x in seq if x not in seen and not seen.append(x)]
In [11]:
print(get_unique_list(l_2d))
In [12]:
print(get_unique_list(l))
In [13]:
print([x for x in set(l) if l.count(x) > 1])
In [14]:
print([x for x in dict.fromkeys(l) if l.count(x) > 1])
In [15]:
print(sorted([x for x in set(l) if l.count(x) > 1], key=l.index))
In [16]:
print([x for x in l if l.count(x) > 1])
In [17]:
def get_duplicate_list(seq):
seen = []
return [x for x in seq if not seen.append(x) and seen.count(x) == 2]
In [18]:
def get_duplicate_list_order(seq):
seen = []
return [x for x in seq if seq.count(x) > 1 and not seen.append(x) and seen.count(x) == 1]
In [19]:
print(get_duplicate_list(l_2d))
In [20]:
print(get_duplicate_list_order(l_2d))
In [21]:
print(get_duplicate_list(l))
In [22]:
print(get_duplicate_list_order(l))
In [23]:
print([x for x in l_2d if l_2d.count(x) > 1])