[Python] Python if statement with "in" condition not working

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying some LeetCode problems and I came across a "Check Duplicates" problem.

    Why doesn't it work when the function receives the array [3,3]?

    Here is my code:

    def containsDuplicate(nums) -> bool:
    list = {}
    for i in range (len(nums)):
    if 3 in list:
    return True
    else:
    list = nums
    print(list)

    return False

    print(containsDuplicate([3, 3]))


    Does anyone know why the "if" statement that's supposed to return True isn't working?

    I know I could use a set or something like a hashmap, but I just want to understand what I'm doing wrong in this case.

    Continue reading...

Compartilhe esta Página