Python Logic Error using a For Each Loop to Remove items from List

Viewed 29

So I have a list of about 300 Tweets captured using Tweepy. Many of these Tweets are Retweets which I want to remove from the list. These can be identified easily as they start with "RT @".

My problem is despite what method I try some of these tweets manage to still get through my loop without being removed. I've tried a few variations of what you see below including the "startswith" function and the "in" operator that I've done below.

Can you see what I'm doing wrong?

def CleanData(tweets):
    removed_tweets = []
    for tweet in tweets:
      if ("RT @") in tweet.full_text:
          tweets.remove(tweet)
          removed_tweets.append(tweet)
    return tweets
1 Answers

The reason why elements are not getting completely filtered from your list is you are using remove operation which deletes the elements from the list dynamically and the list size keeps shrinking. This is causing python to lose track of the element it was on previously and skip an elements every time it removes an element.

Have a look at the output of remove operation I tried on a simple list:

list2=['x','y1','y2','x'] for i in list2: ... print(i) ... if i=='x': ... list2.remove(i)

Output: x y2 x

I suggest you use python filter method on the list . It will simplify your code a lot.

Related