[Tutor] how to only loop over first 'x' items of a list; writing a Twitter bot with tweepy

Alan Gauld alan.gauld at btinternet.com
Mon Nov 25 22:09:08 CET 2013


On 25/11/13 18:18, Jared Nielsen wrote:
> Hi all,
> Noob. For a beginner project I'm hacking together a Twitter bot with tweepy.
>
> I've got one more or less functional, but I'm running into a problem
> when making a list of followers by calling the Twitter api. I'm getting
> a 'Rate limit exceeded' error, which, evidently is due to changes in the
> Twitter api not allowing one to make more than a certain number of
> calls. I think 150 at a time. I have more than 150 followers in the
> account.
>
> My code is:
>
>      for follower in tweepy.Cursor(api.followers).items():
>          follower_ids.append(follower.id <http://follower.id>)
>
> My question is: how do I grab the first 'x' items in
> tweepy.Cursor(api.followers)items(), say 15 or 20, without looping
> through the entire list of items, which then gives me the error.

You could create a slice on the items() return value (assuming its a 
list or similar...

      limit = 20
      for follower in tweepy.Cursor(api.followers).items()[:limit]:
          follower_ids.append(follower.id <http://follower.id>)



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list