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.
CodePudding user response:
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()
CodePudding user response:
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}")
CodePudding user response:
Maybe something like this will work:
results = ("", "", "")
while result != ("success", "success", "success"):
results = function(x)
Assuming that function returns a tuple with three status.