pamela.mg
pamela.mg

Reputation: 11

for loop only returns the last value

I am new to python and I am doing a project on fetching tweets. I was able to do so and now I want to get the hashtags from all those tweets. But when I tried to do a for loop in order to append my list, it only captures the last item/values. I've been all over google and even tried chatGPT but I could not seem to get it right.

Here is my code if anyone is kind to please look and see what I am missing.



keyword = str(input("Enter hashtag"))
date = str(input("Enter date: YYYY-MM-DD"))

tweets = tweepy.Cursor(api.search_tweets, q=keyword, since_id=date, lang='en', tweet_mode = "extended",).items(30) # this is correct

#created columns for Dataframe
columns = ['Date & Time', 'User', 'Retweet_no' ,'Text', 'Location'] 
data_la= [] 

#Created for loop to append data_la 
for tweet in tweets:
  data_la.append([tweet.created_at, tweet.user.screen_name, tweet.retweet_count, tweet.full_text, tweet.geo])
print(tweet.full_text)  
## trying to get hashtags of tweets fetched. 
#Get the tweet text
  tweet_text = tweet.full_text
#Use a regex to find all hashtags in the tweet text
  hashtags = re.findall(r"#(\w+)", tweet_text)
  print('items:', hashtags)
  # Use the entities attribute to get the hashtags

hashtag_list = []
for word in tweets:
     if word == '#':
       hashtag_list.append(word)
print('List:', hashtag_list)

I've been everywhere on google trying to find the answer but to no avail. I have been stuck with this problem for over a week now.

Upvotes: 0

Views: 443

Answers (2)

vince
vince

Reputation: 1643

I think your problem is that with this line:

if word == '#':

you check if the word is only a "#"

You probaly just want to check if the word starts with a hashtag character (as hashtags do). You can do that with the startswith() function wich checks if a string starts with the given character and returns true if it does.

So in youre case your code should probably look like this:

if word.startswith("#"):
    hashtag_list.append(word)

Here you can read a bit more about startswith(): https://www.w3schools.com/python/ref_string_startswith.asp

hope this helps :)

Upvotes: 0

Itération 122442
Itération 122442

Reputation: 2972

As RedzMakersError said, you only check if the word is #, and only #.

You should try something like:

 if word.startswith('#'):
   hashtag_list.append(word)

As the name of the function says, it returns True if the string starts with #, False otherwise.

Official documentation: https://docs.python.org/3/library/stdtypes.html#str.startswith

Upvotes: 1

Related Questions