Question
Python Basic Calculate word length Create a function that returns a dictionary with word lengthsand keys. Use a loop to evaluate each word separately usingthe
Python Basic
Calculate word length
Create a function that returns a dictionary with word lengthsand “keys”. Use a loop to evaluate each word separately usingthe len function. Forexample, len("something") would return 9. Initialize adictionary wl that is the same length as the list ofwords passed to your function and fill the dictionary with thenumber of characters in each word. Use the actual words as the keysfor the dictionary
def word_length(words):
# initialize a dictionary that will hold the
# word length information
# recall that python doesn't have named vectors
# so I'm using a dictionary instead and using
# another loop to create it
wl = {w: 0 for w in words}
# replace with your code
# return the dictionary of word lengths
return wl
word_length(["mouse", "king"]) # should return 5 and 4 in adictionary
word_length(["tax", "house", "purple"]) # should return 3, 5,and 6 in a dictionary
Step by Step Solution
3.41 Rating (160 Votes )
There are 3 Steps involved in it
Step: 1
To calculate the word lengths and store them in a dictionary with the words as keys yo...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started