Home > Software engineering >  Python: JSON, Problem with list of dictionaries, certain value into my dictionary
Python: JSON, Problem with list of dictionaries, certain value into my dictionary

Time:09-15

So, I have been thinking of this for a long time now, but can't seem to get it right. So I have to use a JSON file to make a dictionary where I get the keys: 'userIds' and the value 'completed' tasks in a dictionary. The best I got was the answer: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 90}, with this code under:

import requests


response1 = requests.get("https://jsonplaceholder.typicode.com/todos")
data1 = response1.json()

dict1 = {}
keys = []
values = []

for user in data1:
    if user not in keys or values:
        keys.append(user['userId'])
        values.append(0)


for key, value in zip(keys, values):
    dict1[key] = value



for user in data1:
    if user['completed'] == True:
        dict1[key]  = 1



print(dict1)


but I feel like this next code would be closer, but I can't figure out how to get it to work


import requests

response1 = requests.get("https://jsonplaceholder.typicode.com/todos")
data1 = response1.json()

dict1 = {}
keys = []
values = []

for user in data1:
    if user not in keys or values:
        keys.append(user['userId'])
        values.append(0)


for key, value in zip(keys, values):
    dict1[key] = value


for key, value in data1.items():
    if user['completed'] == True:
        dict1[key].update  = 1


print(dict1)

After this, the output is just " line 24, in for key, value in data1.items(): AttributeError: 'list' object has no attribute 'items'",

And I do get why, I don't jsut know how to continue from here.

Would really appreciate anyones help, with this obnoxious task.

CodePudding user response:

can u try this ?

import requests

response1 = requests.get("https://jsonplaceholder.typicode.com/todos")
data1 = response1.json()

dict1 = {}
keys = []
values = []

for user in data1:
    if user not in keys or values:
        keys.append(user['userId'])
        values.append(0)


for key, value in zip(keys, values):
    dict1[key] = value

print(data1)
for x in data1:
    for key, value in x.items():

        if key =="completed" :
            if value == True:
                dict1[x["userId"]]  = 1


print(dict1)

CodePudding user response:

Try with this approach:

import json
import requests


response1 = requests.get("https://jsonplaceholder.typicode.com/todos")
data1 = response1.json()

dict1 = {}

for user in data1:
    uid = user['userId']
    if user['completed']:
        dict1[uid] = dict1.get(uid, 0)   1
    elif uid not in dict1:
        dict1[uid] = 0

print(dict1)
print(json.dumps(dict1, indent=2))

If needed, you can also simplify the above logic using defaultdict, and leverage the fact that bool is a subclass of int:

from collections import defaultdict

dict1 = defaultdict(int)

for user in data1:
    dict1[user['userId']]  = user['completed']

Output:

{1: 11, 2: 8, 3: 7, 4: 6, 5: 12, 6: 6, 7: 9, 8: 11, 9: 8, 10: 12}
{
  "1": 11,
  "2": 8,
  "3": 7,
  "4": 6,
  "5": 12,
  "6": 6,
  "7": 9,
  "8": 11,
  "9": 8,
  "10": 12
}
  • Related