Stress_
Stress_

Reputation: 400

Iterating over a json object, then adding the results to an array

I'm iterating over a JSON object, then adding each result I get to an array but for each result that gets added to the array, they array is then cleared then it moves down the list of results.

Here's what I mean: enter image description here

Here's the code I used to get this issue:

for e in loadedJson['posts']['entities']:
  print(e)
  titles = []
  titles.append(e)
  print(titles)

Upvotes: 0

Views: 43

Answers (1)

Suryaveer Singh
Suryaveer Singh

Reputation: 597

Try to move titles outside the loop

titles = []
for e in loadedJson['posts']['entities']:
  print(e)
  titles.append(e)
print(titles)

Upvotes: 3

Related Questions