I am writing a for loop that will go through the values in my_dictionary as well as the values in the list. If a word from the list "words" is in the my_dictionary then append the list of values where the word was found once. The code below ran the code but the list look larger than I expected.
my_dictionary = {1: ['apple','dog','cat','bird'],
2: ['mouse','rat','elephant','donkey'],
3: ['tiger','lion','bear','tortoise']}
words = ['apple', 'dog', 'mouse', 'bear','tortoise']
def cen_lst(my_dict, words):
new_lst = []
for key, value in my_dict.items():
for word in words:
if word in value:
new_lst.append(value)
return new_lst
output I expected:
['apple','dog','cat','bird', 'mouse','rat','elephant','donkey', 'tiger','lion','bear','tortoise' ]
Instead the output I am getting is:
[['apple', 'dog', 'cat', 'bird'],
['apple', 'dog', 'cat', 'bird'],
['mouse', 'rat', 'elephant', 'donkey'],
['tiger', 'lion', 'bear', 'tortoise'],
['tiger', 'lion', 'bear', 'tortoise']]