I am trying to parse a JSON output and if a Index has the userId of 5, I want to ignore it. The problem is that it still does not ignore it.
Here is how i read the json (works):
import json
import urllib.request
url = "https://jsonplaceholder.typicode.com/posts"
data = urllib.request.urlopen(url).read().decode()
site_info =json.loads(data)
for info in site_info:
print(f"{}, {}, {}, {}".format(info["userId"], info["id"],
info["title"], info["body"]))
Here is how I prepare a list of userIds to inspect (removal of 5 does not work):
mylist= [{"userId":1}, {"userId":2}, {"userId":3}, {"userId":4}, {"userId":5},
{"userId":6}, {"userId":7}, {"userId":8}, {"userId":9}, {"userId":10}]
for i in range(len(mylist)-5, -5, -5):
print(i)
if mylist[i]["userId"] == 5:
mylist.pop(i)
print (mylist)
I also need to use .pop
as part of the assignment.