Python Friday #115: Working With Twitter Lists in Tweepy

Lists are a helpful Twitter feature that allows us to keep up with interesting accounts. Let’s look at how we can use Tweepy to work with those lists.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Preparation

As with the examples before, we need the user authentication tokens to work on behalf of that user:

 

Create a list

With the method create_list() we can create a new list for our user:

This creates us a new private list:

New list id: 1500259857177452546: News created with Tweepy [0]

If we want to create a public list, we need to set the parameter mode to ‘public’.

 

Add accounts to a list

We can use the method add_list_member() to add accounts one by one:

Current members: 1

Since the number of requests per 15 minutes is limited, we can use the method add_list_members() to add a list of accounts at once (up to 100 per request):

Current members: 3

Currently Twitter does not allow you to add a lot of accounts to a list in a short amount of time. If this happens to you, you will get rate limit errors as described in the last post. In this case you can wait 12 to 24 hours and then reduce the number of accounts you add per hour.

 

Remove an account from a list

We can remove an account from a list with the method remove_list_member()

Current members: 2

As with adding multiple members at once we can remove them in groups with remove_list_members().

 

Show the lists of a user

The method get_lists() shows us the lists of a user:

This gives for my Python Friday account this output:

1500259857177452546 – News created with Tweepy (private|2)
1499136580530941963 – A private list (private|0)
1498823450231771137 – Podcasts (public|4)
1500933128113770505 – News (public|2)

You only can fetch the private lists from your own user, but not from other users.

 

Show the timeline of a list

To show the tweets written by the accounts on your list, we can us the method list_timeline():

This gives us a list of tweets like these:

1500078707964264449 [2022-03-05 12:00:08+00:00] (@PythonNewsread):
Learn Python today, Saturday! https://t.co/Xi1GncLxO9

1499716309164212230 [2022-03-04 12:00:06+00:00] (@PythonNewsread):
Learn Python today, Friday! https://t.co/Xi1GncLxO9

1499580410203942913 [2022-03-04 03:00:05+00:00] (@PythonWeekly):
Python Weekly – Issue 540 https://t.co/qfxPOneLZP #python #django #fastapi #flask #machinelearning #pandas… https://t.co/OdaXpxvfJJ

3

The option include_rts allows us to include (= True) or exclude (= False) retweets, while count limits the number of tweets we get (up to 200).

 

Delete a list

We can delete a list with the destroy_list() method:

If the list exists, the code above will remove it:

1500259857177452546 removed

 

Examples with the V2 endpoint client

I find the V1 API a bit simpler to use when you start learning Tweepy. If you like to see the V2 examples, you can find them in my Python Friday GitHub repository. The files are in the Twitter folder and have a V2 suffix.

 

Next

We can now manage our lists and see what the accounts post. Next week we look at search and how we can find tweets on Twitter.

1 thought on “Python Friday #115: Working With Twitter Lists in Tweepy”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.