0

I am an python newbie trying to create data in json by iterating a for loop like {region1: title1, region2:title2}

Logic:

import json
for region in ["region1","region2","region3"]:
  title="logic to get the title in region"
  data = {region: title}
  # like to update_json.update(data) from second run

I would like the data to be appended with region:title for each loop run. Thanks in advance.

1
  • What's your goal here??? Commented Sep 3, 2021 at 6:01

1 Answer 1

1

Use json.dumps(dict):

import json

dic = {}
for region in ["region1","region2","region3"]:
    title="logic to get the title in region"
    dic[region] = title
j = json.dumps(dic)  # here is
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I have used the logic from stackoverflow.com/questions/18673952/… and it worked. Thank you

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.