Dictionary.update() question

player_to_points ={}
for player, words in player_to_words.items():
  player_points = 0
  for word in words:
    player_points += score_word(word)
  player_to_points.update({player, player_points})

I understand the error exists in the last line within ‘update’ argument. comma must be replaced by a colon.
My question is that I got an error message saying “ValueError: dictionary update sequence element #0 has length 7; 2 is required”
I cannot draw a relation of this error message to the syntax error that I had. Could someone explain it ?

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

1 Like

Kudos to you for trying to hunt down the answers to a specific error code. That will serve you well.

If you check the docs- https://docs.python.org/3/library/stdtypes.html#dict.update
you’ll find that update takes either another dictionary or “an iterable of key/value pairs (as tuples or other iterables of length two)”. For example, zip() works very nicely for this sequence.

If this was passed a dictionary literal {key:value} it’d be grand. Since it’s not it expected a sequence. The sequence it got was whatever player referenced. I’m assuming that was a string seven letters long which was treated as a sequence. So it got 7 items in a sequence rather than the expected two (key, value) style and threw that error.

dict.update is a method, there is no special syntax involved, you would not replace commas with colons
you would however consider what arguments it expects, same as any other method or function