animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}
animals['d'] = ['donkey']
animals['d'].append('dog')
animals['d'].append('dingo')
Be the animals
dictionary defined above. The function applied to it should return the key "d":
biggest(animals)
result: 'd'
My code:
def biggest(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: The key with the largest number of values associated with it
'''
maior = []
key_maior = []
for i in aDict.keys():
#print(i)
key_maior.append(i)
maior.append(len(aDict[i]))
#print(key_maior)
#print(maior)
maximo = max(maior)
#print(maximo)
#print(maior.index(maximo))
return key_maior[maior.index(maximo)]
animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}
animals['d'] = ['donkey']
animals['d'].append('dog')
animals['d'].append('dingo')
print(biggest(animals))
The code works but I think it's very poorly written. Is there another way to solve the problem?