Here's my two methods on this. The first works with a list, maintains the found order of the hashtags and takes care of duplicates. The second works with a set, then returns the set converted to a list.
def extract_hashtags(tags):
"""Return a list of all mentions in text, converted to lowercase,
with duplicates not included
"""
hashtags = []
for word in tags.lower().split():
if word.startswith('#'):
hashtag = word[1:]
if hashtag not in hashtags:
hashtags.append(hashtag)
return hashtags
def extract_hashtags(tags):
# return list({word[1:] for word in tags.lower().split() if word.startswith('#')})
# The above will do it all in one line
# Here's the longer version
hashtags = set() # sets automatically remove duplication
for word in tags.lower().split():
if word.startswith('#'):
hashtags.add(word[1:])
return list(hashtags)
Hope these help.