A gnarly little python loop

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Nov 11 01:23:07 CET 2012

On Sat, 10 Nov 2012 17:58:14 -0500, Roy Smith wrote:

> The way it works, you start with page=1.  It returns a list of tweets.
> If the list is empty, there are no more tweets.  If the list is not
> empty, you can try to get more tweets by asking for page=2, page=3, etc.
> I've got:
>     page = 1
>     while 1:
>         r = api.GetSearch(term="foo", page=page) 
>         if not r:
>             break
>         for tweet in r:
>             process(tweet)
>         page += 1
> It works, but it seems excessively fidgety.  Is there some cleaner way
> to refactor this?

Seems clean enough to me. It does exactly what you need: loop until there 
are no more tweets, process each tweet.

If you're allergic to nested loops, move the inner for-loop into a 
function. Also you could get rid of the "if r: break".

page = 1
r = ["placeholder"]
while r:
    r = api.GetSearch(term="foo", page=page) 
    process_all(tweets)  # does nothing if r is empty
    page += 1

Another way would be to use a for list for the outer loop.

for page in xrange(1, sys.maxint):
    r = api.GetSearch(term="foo", page=page)
    if not r: break


More information about the Python-list mailing list