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
To calculate the word lengths and store them in a dictionary with the words as keys yo... View full answer
Get step-by-step solutions from verified subject matter experts
