Create array of json objects from for loops Create array of json objects from for loops python python

Create array of json objects from for loops


You need to maintain two lists for scores and titles and append all the data to those lists, instead of printing, and then zip those lists along with list comprehension to get the desired output as :

import jsonscores, titles = [], []for line in games_html.findAll('div', class_="product_score"):    scores.append(line.getText(strip=True))for line in games_html.findAll('a'):    titles.append(line.getText(strip=True))score_titles = [{"Title": t, "Score": s} for t, s in zip(titles, scores)]print score_titles# Printing in JSON formatprint json.dumps(score_titles)