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.

  • four
    Try to write more detailed questions. To get an answer, explain exactly what you see the problem. For example, what prevents you from doing this check before adding a record to the list? - Kromster
  • one
    In general, something like this if item not in lst: lst.append(item) - Alexshev92
  • one
    Use sets if the order of the elements is not important - Andrey
  • 2
    And what does "like" mean? Same, or almost, but not quite? - strawdog

1 answer 1

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]