0
roll_number = [47, 64, 69, 37, 76, 83, 95, 97]
sample_dict = {'John': 47, 'Emma': 69, 'Kelly': 76, 'Jason': 97}

for i in list(roll_number):
    if i in sample_dict.values() is False: 
        roll_number.remove(i) #it doesnt come down here, even though it gets 'False' before

print(roll_number)

i in sample_dict.values() returns False, I checked it by printing result of this condition by itself, outside of if statement, but my programm doesn't go to roll_number.remove(i) like it doesn't get False, but it does, so I don't understand what's wrong.

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34

3 Answers3

1

Others have already showed you how to fix this. The problem was this condition:

if i in sample_dict.values() is False

is gonna be interpreted like below(except that sample_dict.values() only gets evaluated once):

if (i in sample_dict.values()) and (sample_dict.values() is False):

They are called chained comparison operators. Read more here.

S.B
  • 13,077
  • 10
  • 22
  • 49
0

The syntax you're looking for is

if i not in sample_dict.values():

Your syntax works as well if we add another pair of parentheses

# Don't do this though
if (i in sample_dict.values()) is False:

This defines the order of the operations to match what you're trying to do -> if True is False:. Otherwise it's two comparisons in a row, which results in unwanted results as stated by @S.B in another answer -> if True and False:.

Also, the cast of roll_numbers to a list in the for loop is redundant - the variable is already a list.

LTJ
  • 1,197
  • 3
  • 16
  • 2
    You **must not** use `is False` (or `is True`). It's logically incorrect and can also fail *in practice*. For example, numpy uses some logical operations that return a proxy class for boolean values test equal to `True` and `False` but which are not object-identical to `True` and `False` (so testing them with `is` *will fail!*). – Konrad Rudolph Oct 10 '22 at 11:23
  • Interesting, I remember learning that `is` should be used with `False`, `True` and `None`. I do get what you're saying but I don't see it as black/white, but dependant of what's the data and purpose of the comparison. As a side note, `flake8` still flags an error when using `== False`. – LTJ Oct 10 '22 at 11:32
  • What purpose would justify reference identity testing rather than value comparison for logical values? I think it's pretty black-and-white (apart from the fact that there's no reason *ever* to explicitly compare logical values, hence flake8's warning). – Konrad Rudolph Oct 10 '22 at 11:43
  • Cool, thanks for pointing it out. To be clear, I'm not recommending anyone to use the second example in my answer here. – LTJ Oct 10 '22 at 12:22
0

You can do this,

for i in roll_number[:]:
    if i not in sample_dict.values():
        roll_number.remove(i)

Output

print(roll_number)
# [47, 69, 76, 97]
  • Use not in
  • Iterating and updating the same list, it's the wrong way of doing it. so iterate through the copy roll_number[:]
Rahul K P
  • 15,740
  • 4
  • 35
  • 52