Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove special characters from a list of elements in python? [closed]

I have a list of elements containing special characters. I want to convert the list to only alphanumeric characters. No special characters. my_list = ["on@3", "two#", "thre%e"]

my expected output is,

out_list = ["one","two","three"]

I cannot simply apply strip() to these items, please help.

like image 212
Vicky Avatar asked Nov 15 '17 07:11

Vicky


1 Answers

Here is another solution:

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub('[^a-zA-Z0-9]+', '', _) for _ in my_list]

output:

['on3', 'two', 'three']
like image 172
Mahesh Karia Avatar answered Sep 28 '22 00:09

Mahesh Karia