python function only return 1 value from dictionary
Question:
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’]
id expect at least all of my canaries…
Answers:
Your return
block needs to have one less indent since that exits the function after the first loop through.
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’]
id expect at least all of my canaries…
Your return
block needs to have one less indent since that exits the function after the first loop through.