Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove special characters from list in python

i have a list like

['hold',  'summit',  'septemb',  '8',  '9',  '.',  "'s",  'nancy-amelia',   'sydney',  '.',  'energy',  ',']

I want to remove all integers, 'dot' , quotes in "s", 'Comma', 'double quote' from such list in one go Or I want to only keep the string entries only i.e hold, summit etc. in the list and remove all other things

Thank in advance

like image 608
Harsh2093 Avatar asked Apr 27 '26 06:04

Harsh2093


1 Answers

>>> lst=['hold',  'summit',  'septemb',  '8',  '9',  '.',  "'s",  'nancy-amelia',   'sydney',  '.',  'energy',  ',']
>>> import re
>>> list(filter(lambda x:x, map(lambda x:re.sub(r'[^A-Za-z]', '', x), lst)))
['hold', 'summit', 'septemb', 's', 'nancyamelia', 'sydney', 'energy']
>>> 
like image 190
riteshtch Avatar answered Apr 29 '26 20:04

riteshtch