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

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Nov 25 19:33:06 CET 2013


On 25/11/2013 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.
>
> Thanks!
>

One way, there are others.

for i, follower in enumerate(tweepy.Cursor(api.followers).items()):
     if i == 20:
         break
     follower_ids.append(follower.id <http://follower.id>)


-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence



More information about the Tutor mailing list