Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python keep only alphanumeric words from list

Tags:

python

list

I have a list of words resembling the following

    mylist=["hi", "h_ello", "how're", "you", "@list"]

I would like to pull out all of the non-alpha numeric characters to give a results such as:

                  "h_ello", "how're", "@list"

Please note I have a much longer list in real life, and it contains some non-alpha numeric instances such as ~, ?, >, =, + etc.

Does anyone know how to do this ,please? Thank you

like image 863
Will.S89 Avatar asked Jan 28 '23 04:01

Will.S89


1 Answers

Use str.isalpha()

Ex:

mylist=["hi", "h_ello", "how're", "you", "@list"]
print([i for i in mylist if not i.isalpha()])

Output:

['h_ello', "how're", '@list']
like image 171
Rakesh Avatar answered Jan 31 '23 11:01

Rakesh