There is a list to which records are added. How to ensure that when adding a new entry to this list, there is a check for the presence of a similar entry in the list. And if a similar entry is in this list, then a new one, which hedgehog is not added.
1 answer
Option number ONCE:
a = [1, 2, 1, 2] b = [] for item in a: if item not in b: b.append(item) print(b) # [1, 2] Option TWO:
a = [1, 2, 1, 2] b = set(a) print(b) # {2, 1} Option THREE (if the order of the elements must be preserved):
a = [2, 1, 1, 2] b = sorted(set(a), key=lambda x: a.index(x)) print(b) # [2, 1] |
if item not in lst: lst.append(item)- Alexshev92