Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python function only return 1 value from dictionary

I am really stuck and could use some help, why is it my function only returns 1 of 15 items from my dictionary list? i thought if i could do an append it would add everything

import requests, json
from importlib import resources
from urllib import response
#from services.canaries.session import create_session_canary
    
def canaries_up_time(results):

    results = results.json()
    #print(json.dumps(my_dict, indent=3))
    for item in results['devices']:
        newlist = []
        newlist.append(item.get("name"))
        name = item.get("name")
        ipaddress = item['ip_address']
        uptime = item['uptime']
        location = item['location']
        
        print(newlist)
        return newlist


def get_contained_hosts_list():
    #session = create_session_canary()
    canary_auth='key'
    
    url = f"https://domain.canary.tools/api/v1/devices/live"
    payload = {
    'auth_token': canary_auth
    }
    results = requests.get(url, params=payload)

    return canaries_up_time(results)


if __name__ == '__main__':
    get_contained_hosts_list()

Output

[‘D14-GC-DMZ1’]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

id expect at least all of my canaries…

>Solution :

Your return block needs to have one less indent since that exits the function after the first loop through.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading