Find Twitter Following Users Who Do Not Belong To a List
A Twitter user management strategy is to group users into list. However, if a user isn't immediately assigned to a list or lists when initially following them, there is no way in the Twitter user interface to determine who is or is not on a list at a later time.
The following code snippet makes use of a Python wrapper around the Twitter API to create a list of users an account is following who have not been added to a list. Assuming the number of users who don't yet belong to a list is manageable, they can then be manually added to the appropriate list (here the code is outputting a screen name which can be queried in-browser to manually call up the user's page and use the tools on the page to add them to a list). The information returned could just as well be used to programmatically add them to an existing list or make a new list depending on the specific use case.
"""Print the usernames of Users that we are following who have not been assigned to a list. Prints nothing if everyone is on a list. """ import twitter # https://github.com/bear/python-twitter api = twitter.Api( consumer_key='', # Fill in values per https://github.com/bear/python-twitter#api consumer_secret='', access_token_key='', access_token_secret='' ) # the twitter.models.User class does not by default accommodate comparison of # objects, we can assume that the User.id attribute will always be unique per # https://developer.twitter.com/en/docs/basics/twitter-ids and, as such, # use the User.id attribute for comparison between User class instances def __eq__(self, other): if isinstance(other, self.__class__): return self.id == other.id else: return False def __ne__(self, other): return not self.__eq__(other) # override the inherited __eq__() and __ne__() methods of twitter.models.User twitter.models.User.__eq__ = __eq__ twitter.models.User.__ne__ = __ne__ # get collections of everyone we are following and our lists users = api.GetFriends() lists = api.GetLists() # create an empty Python list to store a succinct list of User objects # belonging to any and all lists listed_members =  # to begin with, listed_members will be a list of lists of members from each # Twitter list i.e. [[Users from the 1st Twitter list], [Users from the 2nd]] [listed_members.append(api.GetListMembers(list_id=list.id)) for list in lists] # flatten the list of member lists down to one large list of User objects # i.e. [User1, User2, User3] flat_member_list = [item for sublist in listed_members for item in sublist] # iterate over the list of followers and check membership, printing the # User.screen_name if membership does not exist for user in users: if user not in flat_member_list: print(user.screen_name)
Email us at email@example.com.