I am writing a program to create a dictionary in python were the key will be the lenghth of words and the value will be the words itself. So therefore, if i have say a word like "in" and "to", i expect the dictionary to print dict_value = {2:["in","to"]}. To further clarify my problem here is my code:
string_value = "I ate a bowl of cereal out of a dog bowl today."
remove_p = string_value.replace(".","")
remove_c = remove_p.replace(",","")
remove_q = remove_c.replace("?","")
remove_e = remove_q.replace("!","")
remove_a = remove_e.replace('\'',"")
lowervalue = remove_a.lower()
split_v = lowervalue.split()
length = {}
for i in split_v:
length[len(i)] = []
length[len(i)].append(i)
print length
This is what my code is printing:
{1: ['a'], 2: ['of'], 3: ['dog'], 4: ['bowl'], 5: ['today'], 6: ['cereal']}
This is what i want it to print:
{1: ['i', 'a', 'a'], 2: ['of', 'of'], 3: ['ate', 'out', 'dog'], 4: ['bowl', 'bowl'], 5: ['today'], 6: ['cereal']}
So, if a word has the same length it should be printed under the same keys. Thanks for your help in advance. This question is about displaying words with the same length under one key and does not involve refactoring. I have reviewed similar questions but no perfect fit for mine. They are either too advanced or too basic