FAQ: Code Challenge: Dictionaries - Word Length Dict

This community-built FAQ covers the “Word Length Dict” exercise from the lesson “Code Challenge: Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:
https://www.codecademy.com/courses/learn-python-3/lessons/python-functions-dictionaries-cc/exercises/word-length-dict

FAQs on the exercise Word Length Dict

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: Could this be solved with list comprehension?

3 posts were split to a new topic: Why is .append() working with my code?

2 posts were split to a new topic: How does the provided solution create new keys?

A post was merged into an existing topic: Why isn’t .append() working with my code?

I can’t wrap my head around the code below. Can someone please enlighten me?

How is it possible that when the code newdict[word] = len(word) runs, it knows that it’s the VALUE and not the KEY that it needs to update?

    newdict[word] = word
    newdict[word] = len(word)
def word_length_dictionary(words): 
  newdict = {}
  for word in words: 
    newdict[word] = word
    newdict[word] = len(word)
  return newdict

Also, I’m still trying to get a good grip on Dictionaries and although I might be able to finish this topic, I may not be confident enough to move forward. Do you suggest I re-take the whole practice? Should I read up/watch from other resources online to solidify my knowledge? I welcome any suggestions. Thank you for your help.

“updating” a key would mean removing one key and inserting another
changing it in-place does not make sense. the key is the place.

(also that would be a useless dict, if you have a word then you have the length as well, there’s no reason to create a lookup table for that)

def word_length_dictionary(words):
  dct = {}
  for i in words:
    dct[i] = len(i)
  return dct
def word_length_dictionary(words):

  return {word: len(word) for word in words}

image
why isn’t this code working?

Double check the location of your return statement (indentation is important).

It may also be worth reconsidering the location of dict[word] = len(list) as it’s currently indented to the same level as where you create the list so it changes on every iteration which is unnecessary. You could also consider using len directly on a string since that is supported.

<3

Slightly off topic:

An inverse to the challenge…

def inverse_word_length(words):
  word_lengths = {}
  for word in words.split():
    key = f"{len(word)}"
    if key not in word_lengths.keys():
      word_lengths[key] = [word]
    else:
      word_lengths[key] += [word]
  return dict(sorted(word_lengths.items()))
print (inverse_word_length("A quick brown fox jumps over the lazy dog"))
{'1': ['A'], '3': ['fox', 'the', 'dog'], '4': ['over', 'lazy'], '5': ['quick', 'brown', 'jumps']}

Just for the fun of it!

1 Like