I have some code like:
def example(*, current_age:int):
my_final_return={}
const1 = int(40)
const2 = int(45)
const3 = int(50)
ages_list = (current_age, const1, const2, const3)
new_list = ()
for i in ages_list:
new_list += (i,)
for i in new_list:
my_final_return[i] = {
"current_age": i * 2
}
return my_final_return
When I try using this code with a current_age
that matches one of the constant values, I only get three keys in the result:
>>> example(current_age=40)
{40: {'current_age': 80}, 45: {'current_age': 90}, 50: {'current_age': 100}}
I read Duplicate values in a Python dictionary, so I understand that it is not possible to have duplicate keys in the dictionary. To work around this, I tried returning a tuple instead. This allowed me to duplicate "keys", but I only got (40, 40, 55, 50)
as the result - the current_age
information is missing.
This code will be part of a web API implementation (using FastAPI), so I need to return something that can be converted to JSON.
How can I structure the data to return all the information?