How to reduce the iteration of this loop

Viewed 40

I have the below loop in my program which check for two arrays. If value in "status" array is FAILED then it picks the uniqueid from the other array and run the api call. Both the array had 99 values in them.

count = 0
count1 = 0
for x in range(len(status)):
    for y in range(len(uniqueid)):
        t = status[x]
        g = uniqueid[y]
        count1 += 1
        if t == "FAILED":
            api_endpoint = "https://dummywebsite.com/XX/XXX/4754/history/"+str(g)+"/data/catch"
            param = {'access_token':access}
            api_archive = requests.put(url= api_endpoint, params = param)
            count += 1
if count > 0:
    print(count,"data recovered")
else:
    print("No data are in FAILED state")

print(count1) 

I just tried checking how many times this loop ran by adding count1 variable. It printed the value 9801, which means my loop ran for that many times. what i can do to reduce those numbers? I am fairly new to python.

1 Answers

You don't need to loop on x, and y separately. Also you are counting double each time t=="failed". Is not clear if that's is what you want.

for t,g in zip(status, uniqueid):
    count1 += 1
    if t == "FAILED":
        api_endpoint = f"https://dummywebsite.com/XX/XXX/4754/history/{str(g)}/data/catch"

        param = {'access_token':access}
        api_archive = requests.put(url= api_endpoint, params = param)
        count += 1
Related