You can use regex for removing any word starting with @
like this :
import re
aa = re.sub(r"@\w+", "", mm).strip()
aa
would be this string: "I can't describe my feelings."
If you don't want to use regex, you can split the string into words and filter on basis of the first char and then rejoin, like this :
aa = ' '.join(i for i in mm.split() if not i.startswith('@')).strip()
This should also give the same desired output string "I can't describe my feelings."
Note : The reason your code is failing to do what you want it to do is that you're modifying the list while iterating through it. In the first iteration it gets the word at 0th index @sam_mie_J
and removes it. Now in the second iteration it's 0th item is @4N_McrAirport
and 1st item is @airfrance
but it is trying to remove the item at 1st index - which is actually @airfrance
. You can modify your input string to the following :
m2 = "@sam_mie_J @user2 @user3 @user4 @user5 @user6 @4N_McrAirport @airfrance I can't describe my feelings."
to understand this better. You'll notice multiple words starting with @
are being missed by your code.
The minimal change you can do in your code is to make a shallow copy of the list while iterating through it like this :
for jj in aa[:]: