Why isn't .append() working with my code?

.append() doesn’t appear to work here. Error says keyword can’t be an expression but in the solution it is. Can anyone clarify?

Thanks!

# Write your word_length_dictionary function here:
def word_length_dictionary(words):
  dict={}
  for i in words: 
    dict.append([i]=len(i))
  return dict 
# Uncomment these function calls to test your  function:
print(word_length_dictionary(["apple", "dog", "cat"]))
# should print {"apple":5, "dog": 3, "cat":3}
print(word_length_dictionary(["a", ""]))
# should print {"a": 1, "": 0}

Review how you add a key:value pair to a dictionary:

dictionary_name[key] = value

That’s all. append() is a list method, not used with dict.

2 Likes

Try this , If you want to use append:

def word_length_dictionary(words):
length =
for i in words:
length.append(len(i))
z = {l:n for l,n in zip(words,length)}
return z

def word_length_dictionary(words):
value=
for word in words:
value.append(len(word))
new = {key:value for key,value in zip(words,value)}
return new