简体   繁体   中英

Loop a function until all elements in a python list have a specific value

I would like to loop a function until all elements in my list have a specific value but I have no idea how to achieve this outcome.

basically, I'm creating a list based on an API call and I need to check if all elements have "success", if not I want to run the function again until I have all elements with success status and continue my script.

list example:

list = ['init', 'init', 'success'] - need to repeat the function
list = ['init', 'success', 'success'] - need to repeat the function
list = ['success', 'success', 'success'] - Ready to go! continue the script

basically, I'm struggling with the loop and then calling the function.

Can anyone help me with that?

Cheers.

One possible approach is to use a while loop until the function returns the desired result.

myList = ['init']
while any(it != 'success' for it in myList):
    my_list = functionCall()

Maybe something like this will work:

results = ("", "", "")
while result != ("success", "success", "success"):
   results = function(x)

Assuming that function returns a tuple with three status .

Great, thanks for the tip about any() method. that was exactly what I need!

check_list = []
for a in sa_response_json['items']:
    check_list.append(a['status']['state'])

print(f"-------------------- {check_list}")

if any(it != 'success' for it in check_list):
    print("Checking devices status = Success. It can take some time")
    sleep(10)
    print("Checking again")
    check_agent_state()
else:
    print("-------------------- All devices have been onboarded")
    print(f"-------------------- {check_list}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM