How to trigger something on the close of a loop?
Question: Is this possible? I want to print lines in my file 5 at a time (to send to an
API in a batch). But when I get to the last few lines they never print because
there are less than 5, never triggering my if statement to print. SO I figured
one way to tackle this is to print the remaining lines when the loop closes.

The current code is messy and redundant but this is the idea:

    
    
    urls = []
    urls_csv = ""
    counter = 0
    
    with open(urls_file) as f:
        for line in f:
    
            # Keep track of the lines we've went through
            counter = counter + 1
    
            # If we have 5 urls in our list it's time to send them to the API call
            if counter > 5:
                counter = 0
                urls_csv = ",".join(urls) # turn the python list into a string csv list
                do_api(urls_csv) # put them to work
    
                urls = [] # reset the value so we don't send the same urls next time
                urls_csv = "" # reset the value so we don't send the same urls next time
             # Else append to the url list
             else:
                urls.append(line.strip))
    

Also - Generally speaking, is there a better way to tackle this?


Answer: You can group them into sets of 5 lines at a time with the [`itertools`
grouper recipe](https://docs.python.org/3/library/itertools.html#itertools-
recipes).

    
    
    import itertools
    
    def grouper(iterable, n, fillvalue=None):
        args = [iter(iterable)] * n
        return itertools.zip_longest(*args, fillvalue=fillvalue)
    
    with open(...) as f:
        for group in grouper(f, 5, fillvalue=""):
            do_api(",".join([g.strip() for g in group if g]))
    

