Home > OS >  Add item back to for loop
Add item back to for loop

Time:01-26

I am working on some code, and I cannot figure out how to create the behavior that I want. I want to iterate through a list, and pull some data. In my real code, the code is designed to pull some data, but the data is posted at different times and not really in a predictable fashion. What I'd like to do is add the iterable back into the list if there is an error.

Example Code

my_list = ['cat', 'dog', 'bird', 'fish']


for animal in my_list:
    try:
        get.('www.somerandomurl_{}.com'.format(animal))
        print("good work!")
    except:
        add item back to list, try again once the rest of the list is compete

Is there a way to do this? Possibly even telling python to wait for n minutes to try again with the items that had an error

CodePudding user response:

You can use a loop that will run until you've fetched all the items, at each run select (and pop) an element:

  • if success: you're good
  • if fail : put it back in the values to try
from requests import get
from collections import deque

my_list = ['cat', 'dog', 'bird', 'fish']
to_fetch = deque(my_list)

while to_fetch:
    to_try = to_fetch.popleft()
    try:
        res = get('www.somerandomurl_{}.com'.format(to_try))
        print("good work!", res.text)
    except Exception:
        print("Failed", to_try, "but will try again later")
        to_fetch.append(to_try)
  •  Tags:  
  • Related