0

When I run the code without the 'is True' code at the end of my elif statement, the correct list is produced; however, when I add it, an empty list is printed. I have also tried equating the if statement to the boolean True; however, that too doesn't work (and PyCharm tells me that there is a minor error - amber). I want to understand why this is happening.

numbers_list = [5, 2, 546, 7, 3, 5, 2, 7, 29, 6, 5, 7]
uniques = []
for number in numbers_list:
    if number in uniques:
        continue
    elif number not in uniques is True:
        uniques.insert(-1, number)
print(uniques)
ami
  • 1
  • 3
  • 1): `is` is different from `==`. 2): `number not in uniques` is sufficient, no need for the `is True` afterwards (and likely you're missing parentheses there to ensure operator precedence) – GPhilo Jul 23 '20 at 08:56
  • @GPhilo true, but that isn't the reason. `==` would fail too. – juanpa.arrivillaga Jul 23 '20 at 08:59
  • also you could use ***set*** to get unique numbers – LearningNoob Jul 23 '20 at 09:00
  • THis is failing because `in` and `is` are both *comparison operators*, thus, comparison operator chaining occurs, so `elif number not in uniques is True:` is equivalent to `elif number not in uniques and uniques is True:` and `uniques is True` is never true... Note, comparison chainging allows you to write things like `0 < x < 20`, which will be equivalent to `0 < x and x < 20` – juanpa.arrivillaga Jul 23 '20 at 09:00
  • @juanpa.arrivillaga I didn't say that was the reason the code is broken. I'm just pointing out that the OP likely confuses the two operators: `[] == []`, but `[] is not []` (both these expression evaluate to True). – GPhilo Jul 23 '20 at 09:02
  • Thank you @GPhilo, but how is `is` different from `==` ? I'm quite new to coding so maybe it is a silly question. Also, I know that `number not in uniques` works but I don't understand why. I tried adding the parentheses but I got a SyntaxError. – ami Jul 23 '20 at 09:03
  • @ami: That's a very-often asked question (with many duplicates, too): https://stackoverflow.com/questions/15008380/double-equals-vs-is-in-python – GPhilo Jul 23 '20 at 09:05
  • @ami that isn't really related to this question *per se*, but it is very important. `is` tests for *object identity*, and `==` tests for *equality*. So suppose I make two different lists, `a = [1, 2, 3]` and `b = [1, 2, 3]`. Then `a == b` but `a is not b`, see [this question](https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is) – juanpa.arrivillaga Jul 23 '20 at 09:05
  • Note, you *almost never* need `is True`. The only time you want that is if you literally want to check if something is the singleton object `True`. Also, consider, `1 == True` will be true but `1 is True` will be false – juanpa.arrivillaga Jul 23 '20 at 09:06
  • @juanpa.arrivillaga, thank you, the links answer my questions, but why is it that `unique ==True` can never be True? I understand why `unique is True` can't be True as they don't have the same id, but if the value of the unique list is 1 should it not be True? – ami Jul 23 '20 at 10:28
  • @ami how can a list equal a number? In python, a list is never equal to a number. That comparison always returns False. Note, in Python, objects can control how to define equality, `==`, by implementing a `__eq__` method. But the built-in types always return `False` when compared to dissimilar types. – juanpa.arrivillaga Jul 23 '20 at 10:40

1 Answers1

-1

As most have answered the issue with your code, i am proposing an alternate solution for a unique list with order preserved. Use dict.fromkeys()

print(list(dict.fromkeys(numbers_list)))

This will give you the same result of a unique list and preserve the order as well

vestronge
  • 897
  • 6
  • 10
  • There's nothing wrong with posting an answer that provides an alternative solution to the problem. However, you should ensure that it actually does solve the problem, and show how it does so. – Cody Gray - on strike Jul 23 '20 at 18:50