Can only concatenate list (not "int") to list (Spam filter project)

Screen Link:
My code: https://app.dataquest.io/m/433/guided-project%3A-building-a-spam-filter-with-naive-bayes/7/calculating-parameters
Solution: https://github.com/dataquestio/solutions/blob/master/Mission433Solutions.ipynb

My Code:
when calculating parameter p(secret / spam) I get the error: can only concatenate list (not “int”) to list
So I tried to find the problem by first scanning the values of:
‘’’
z =
for w in vocabulary:
y = ham_count[w].sum()
z[w].append(y)
‘’’
and then I got the error: list indices must be integers, not str

However, my code looks the same compared with the solution provided >> Chapter Calculating Parameters, The Finale training set and Calculating Constants First.

So, could someone please help me out please? Many thanks!

Replace this line with your code

What I expected to happen:
Finishing first part of the algorithm

What actually happened:
error

Replace this line with the output/error
can only concatenate list (not "int") to list

<!--Enter other details below: -->

hi @atbheeremans

Welcome back to DQ Community!

Not entirely sure about comparison with this part,

I am assuming vocabulary and z are both lists here and the Vocabulary list is a collection of words. So the code
for w in vocabulary: translates to for each word that exists in the vocabulary list.

the variable w can then take values like “hello”, “chat”, “won”, “lottery” etc.

when the parser comes across this, this will get translated to z["hello"].append(y) which python list is unable to handle, as the list expects an integer index value and not a string.

Even if we replace w with an integer say 0, then also the code won’t work, as z[0] will give us the first element in the list (suppose that z list has some elements) and then to that element value which could be str type or int type you are trying to apply append() method which is not applicable to these datatypes.

Hope that helps.

ah I see, alright many thanks. I always find it difficult to find the differences between a list and dictionary.