CDNthe2nd
CDNthe2nd

Reputation: 369

Python - printing out through json objects using for loop

So I have been playing around to make a sort of monitor - What monitor is that it prints out whenever a new item, object, number is new, then print it out basically nothing harm.

The json I am printing out (I know this is not a correctly json format now but it does print out the correctly stuff currently)

"threads": {
    "1": {
        "name": "Hello",
        "id": "4174"
    },
    "2": {
        "name": "World",
        "id": "6231"
    },
    "3": {
        "name": "Overflow",
        "id": "7231"
    }
}

Basically I have done a script that right now:

def get_feed():

    url = 'https://www.helloworld.com/loadfilter'
    resp = s.get(url, timeout=6)
    resp.raise_for_status()

    return resp.json()['threads']

old_list = []

for index, thread in get_feed().items():
    old_list.append(thread['id'])

while True:
    try:

        new_list = []
        for index, thread in get_feed().items():
            new_list.append(thread['id'])

        for index, item in enumerate(new_list):
            if item in old_list:
                print(item['name'] # ERROR - string indices must be integers

        else:
            print('Sleeping 2 sec')
            time.sleep(2)


    except Exception as e:
        print(e)


sys.exit()

So basically if I print out inside the for index, thread in get_feed().items(): a print of thread['name'] that would be not an issue and it would print it out.

However in the for loop of: if item in old_list: - The issue there is it does not print out anything but the id numbers that I have added to the list and I wonder how can I possible make it so it prints out the names through the json that is given above?

Meaning : whenever I print out after the if item old list etc. item['name'] it should give the name?

Upvotes: 0

Views: 97

Answers (2)

pekapa
pekapa

Reputation: 891

Your item is just the thread['id'] and that's why you can't access the name anymore. You could try saving the whole thread object in your lists and not just the id

For instance:

for index, thread in get_feed().items():
    new_list.append(thread)

or, as suggested in @henry's answer, without the loop:

new_list = list(get_feed().values())


And than you can compare if item['id'] is in the old_list and than print (item['name']) would work because you'd have the whole thread object.

(Edit: note that, here, we are only checking the id, if you go back to @henry's suggestion, you would check the whole thread, including every attribute that it might have)

Upvotes: 1

Henry Woody
Henry Woody

Reputation: 15672

The problem with your code is that when you create old_list you are only appending the id of each thread, rather than the thread itself. If you want to be able to print the name of the thread, you'll need to put each thread object in old_list. You should also do the same thing in creating new_list in order for everything to match up.

Here's how you can do this:

Creating old_list (note there's no need to use a for loop to get the values out of a dictionary):

old_list = list(get_feed().values())

Creating new_list:

new_list = list(get_feed().values())

Checking/printing:

for thread in new_list:
    if thread in old_list:
        print(thread['name'])

Upvotes: 1

Related Questions