-2

I just started learning python and found this snippet. It's supposed to count how many times a word appears. I guess, for all of you this will seem very logical, but unfortunately for me, it doesn't make any sense.

str = "house is where you live, you don't leave the house."
dict = {}
list = str.split(" ") 
for word in list:  # Loop over the list
    if word in dict:  # How can I loop over the dictionary if it's empty?
        dict[word] = dict[word] + 1
    else:
        dict[word] = 1

So, my question here is, how can I loop over the dictionary? Shouldn't the dictionary be empty because I didn't pass anything inside? Maybe I am not smart enough, but I don't see the logic. Can anybody explain me how does it work? Many thanks

glibdud
  • 7,550
  • 4
  • 27
  • 37
Puno
  • 1
  • 4
    the dict is getting "filled" while the loop runs. it is only empty for the very first pass. Also, never use keywords for variable names. str, dict and list are being overwritten here. – Paritosh Singh Jan 23 '19 at 18:06
  • 2
    You added a comment about loops with `if word in dict:`. The `if` statement is not a loop. – TigerhawkT3 Jan 23 '19 at 18:07
  • 1
    (And anyway, it's perfectly fine to loop over an empty collection, just like it's perfectly fine to multiply numbers by 0.) – user2357112 Jan 23 '19 at 18:09
  • 2
    @ParitoshSingh - `str`, `dict`, and `list` aren't keywords; they're just built-in functions. If they were keywords you'd get an error when trying to assign to them. That's why caution is required when considering names: it won't automatically catch that for you. – TigerhawkT3 Jan 23 '19 at 18:09
  • My bad, thanks. Wish i could edit my comment. – Paritosh Singh Jan 23 '19 at 18:12
  • 1
    Thanks, everybody for your input. I have learnt more from you that focusing for over one hour on this piece of code by myself. – Puno Jan 23 '19 at 18:17
  • You are looping over a list, not a dictionary. You test if an element (word) exists in a dictionary. If not exists, you insert it with value 1. It exists, you are adding 1 to the value – nacho Jan 23 '19 at 18:24

1 Answers1

-2

As somebody else pointed out, the terms str, dict, and list shouldn't be used for variable names, because these are actual Python commands that do special things in Python. For example, str(33) turns the number 33 into the string "33". Granted, Python is often smart enough to understand that you want to use these things as variable names, but to avoid confusion you really should use something else. So here's the same code with different variable names, plus some print statements at the end of the loop:

mystring = "house is where you live, you don't leave the house."
mydict = {}
mylist = mystring.split(" ") 
for word in mylist:  # Loop over the list
    if word in mydict:  
        mydict[word] = mydict[word] + 1
    else:
        mydict[word] = 1
    print("\nmydict is now:")
    print(mydict)

If you run this, you'll get the following output:

mydict is now:
{'house': 1}

mydict is now:
{'house': 1, 'is': 1}

mydict is now:
{'house': 1, 'is': 1, 'where': 1}

mydict is now:
{'house': 1, 'is': 1, 'where': 1, 'you': 1}

mydict is now:
{'house': 1, 'is': 1, 'live,': 1, 'where': 1, 'you': 1}

mydict is now:
{'house': 1, 'is': 1, 'live,': 1, 'where': 1, 'you': 2}

mydict is now:
{"don't": 1, 'house': 1, 'is': 1, 'live,': 1, 'you': 2, 'where': 1}

mydict is now:
{"don't": 1, 'house': 1, 'is': 1, 'live,': 1, 'leave': 1, 'you': 2, 'where': 1}

mydict is now:
{"don't": 1, 'house': 1, 'is': 1, 'live,': 1, 'leave': 1, 'you': 2, 'where': 1, 'the': 1}

mydict is now:
{"don't": 1, 'house': 1, 'is': 1, 'live,': 1, 'house.': 1, 'leave': 1, 'you': 2, 'where': 1, 'the': 1}

So mydict is indeed updating with every word it finds. This should also give you a better idea of how dictionaries work in Python.

To be clear, you're not "looping" over the dictionary. The for command starts a loop; the if word in mydict: command isn't a loop, but just a comparison. It looks at all of the keys in mydict and sees if there's one that matches the same string as word.

Also, note that since you only split your sentence on strings, your list of words includes for example both "house" and "house.". Since these two don't exactly match, they're treated as two different words, which is why you see 'house': 1 and 'house.': 1 in your dictionary instead of 'house': 2.

Bill M.
  • 1,388
  • 1
  • 8
  • 16