DEV Community

loading...

Discussion on: Adding values to an empty list

Collapse
fronkan profile image
Fredrik Bengtsson

So I think the issue is the use of the not equals operator (!=). It checks if the thing to the left is not equal to the thing on the right. If I understood your example the check if statement with values witten out would be:

if 3 != [3]:
    print("My unique list:")
    ...

Because, the value on the left is a number and the the value to the right is a list containing the value 3 they will never be equal.

Instead python as two other operators in and not in. Which can be used to check if a values exist in a list (or other container supporting it). In this case the is statement would be:

if X not in myUniqueList:
    ...

Hope this was helpful, feel free to ask any questions if I was unclear😊

Collapse
itchyonion profile image
Jon Obi Author

Oh, this was really helpful. I was totally unaware of these other operators. The code works fine now. Thanks again!.