I have below function where I am expecting the return as a dictionary with key and having multiple values.what is wrong in below code?
def get_list_animals():
output = {}
animal_house = ['ZOO','JUNGLE']
connection = Connection("WORLD")
cursor = connection.cursor()
for house in animal_house:
statement = """select animals from inventory where place = '{}'""".format(house.upper())
cursor.execute(statement)
rows = cursor.fetchall()
for row in rows:
values = str(row).strip('()')
if house == 'ZOO' or 'JUNGLE':
output[house] = values
#print(output)
return output
answer = get_list_animals()
print(answer)
Return value:
{'ZOO': 'GOAT', 'JUNGLE': 'HORSE'}
whereas I am getting lots of values for both ZOO & JUNGLE in below way if I print
ZOO:
{'ZOO': 'BEAR'}
{'ZOO': 'MONKEY'}
{'ZOO': 'MULE'}
JUNGLE:
{'ZOO': 'MULE', 'JUNGLE': 'TIGER'}
{'ZOO': 'MULE', 'JUNGLE': 'ELEPHANT'}
{'ZOO': 'MULE', 'JUNGLE': 'HORSE'}
I am expecting a single dictionary as a return
{'ZOO': 'BEAR','MONKEY','MULE','JUNGLE': 'TIGER','ELEPHANT','HORSE'}