0

So I'm a beginner in python and I'm doing a task that needed me to count how many items in a list, count each item and put it into dictionary. At the end I did managed to make the program work but I don't understand why this code doesn't work

def stringinlist(*strings):
    dictionary = {}
    count = 0
    for objects in strings:
            if objects in dictionary == False:
                for check in strings:
                    if objects == check:
                        count += 1
                dictionary[objects] = count
            count = 0
    return dictionary

print(stringinlist("yo","hello","hello","yo"))

I tried to debug and saw that the "if objects in dictionary.." doesn't work even when its supposed to return False any explanation?

Nir Cohen
  • 25
  • 8
  • Please read [mcve] - the expected result would be nice. – wwii Feb 18 '18 at 23:03
  • 3
    You're a victim of operator chaining. `objects in dictionary == False` is evaluated as `(objects in dictionary) and (dictionary == False)`. Use `if objects not in dictionary:` instead. – Aran-Fey Feb 18 '18 at 23:05
  • 1
    Better yet `if objects not in dictionary:`, to keep the sense of the test. – alexis Feb 18 '18 at 23:06
  • @Aran-Fey, Tried that but Instead of returning {yo:2,hello:2) its returning {} That's basically what I did alone and because I saw that nothing happens I used else and then it worked but its seems stupid to use else everything I want to check if something is false – Nir Cohen Feb 18 '18 at 23:12

0 Answers0