0
@app.get("/")
async def read_root():
# houses = json.load(open("data.json"))["houses"]["house"]
# return houses[0]["id"]

houses = json.load(open("data.json"))["houses"]["house"]
for house in houses:
    return house["id"]

When I run the commented code, I receive "3070", which is working as intended, however when I try to loop it in the non-commented code, I also receive "3070" instead of a list of ids.

Can't figure this one out, thanks for the help :)

1
  • the first execution of the return statement ends the code which means it should be outside the for..loop Commented Dec 9, 2022 at 12:42

1 Answer 1

2

The return statement ends the loop and the function. Your loop is correct but it will stop at the first element because of return. This is probably what you want:

houses = []
for house in json.load(open("data.json"))["houses"]["house"]:
  houses.append(house["id"])

return houses
Sign up to request clarification or add additional context in comments.

1 Comment

Disregard my previous comment, IT WORKED PERFECTLY !! Many thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.