How to remove special characters from a list in python?

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.

asked Nov 15, 2017 at 7:36

3

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']

answered Nov 15, 2017 at 7:46

Mahesh KariaMahesh Karia

2,0151 gold badge11 silver badges21 bronze badges

Use the str.translate[] method to apply the same translation table to all strings:

removetable = str.maketrans['', '', '@#%']
out_list = [s.translate[removetable] for s in my_list]

The str.maketrans[] static method is a helpful tool to produce the translation map; the first two arguments are empty strings because you are not replacing characters, only removing. The third string holds all characters you want to remove.

Demo:

>>> my_list = ["on@3", "two#", "thre%e"]
>>> removetable = str.maketrans['', '', '@#%']
>>> [s.translate[removetable] for s in my_list]
['on3', 'two', 'three']

answered Nov 15, 2017 at 7:43

Martijn PietersMartijn Pieters

984k273 gold badges3872 silver badges3234 bronze badges

3

try this:

l_in = ["on@3", "two#", "thre%e"]
l_out = [''.join[e for e in string if e.isalnum[]] for string in l_in]
print l_out
>['on3', 'two', 'three']

answered Nov 15, 2017 at 7:46

Using two for loops

l = ['@','#','%']
out_list = []
for x in my_list:
    for y in l:
        if y in x:
            x = x.replace[y,'']
            out_list.append[x]
            break

Using list comprehension

out_list = [ x.replace[y,'']  for x in my_list for y in l if y in x ]

Assuming 3 in on@3 is a typo, the output will be on@3 and not one as expected

answered Nov 15, 2017 at 7:39

Van PeerVan Peer

2,0672 gold badges22 silver badges34 bronze badges

5

In this article, we will discuss four different ways to delete special characters from a string in python.

In Python the strings are immutable. It means we can not change its contents. But we can create a new string with only a few selected characters from the original string. Then we can assign back this new string to the original variable. It will give an effect that string has been modified and unwanted characters have been deleted from it.

Let’s see different ways to delete special characters from a string,

In python, string.punctuation from string module contains all the special characters i.e.

r"""!"#$%&'[]*+,-./:;[email protected][\]^_`{|}~"""

We can use this to create a regex pattern, that will match all the special characters in a string. Then using the sub[] function of regex module, we can replace all the special characters with an empty string. Let’s understand with an example,

Advertisements

import string
import re

sample_str = "Test&[88]%%$$$#$%-+String"

# Create a regex pattern to match all special characters in string
pattern = r'[' + string.punctuation + ']'

# Remove special characters from the string
sample_str = re.sub[pattern, '', sample_str]

print[sample_str]

Output:

Test88String

It removed all the special characters from the string.

Another approach:

Instead of looking for special characters in string and removing them. We can look for all alphanumeric characters and remove the remaining characters. For example,

import re

sample_str = "Test&[88]%%$$$#$%-+String"

# Create a regex pattern to match all characters except letter or numbers
pattern = r'[^A-Za-z0-9]+'

# Remove special characters from the string
sample_str = re.sub[pattern, '', sample_str]

print[sample_str]

Output:

Test88String

It also removed all the special characters from the string.

Remove special characters from a string using List comprehension and join[]

Using list comprehension, iterate over all the characters of string one by one and skip characters non alphanumeric characters. It returns a list of filtered characters. Combine these remaining characters using join[] and assign it back to same variable. It will give an effect that we have deleted all special characters from the string. For example,

sample_str = "Test&[88]%%$$$#$%-+String"

# Remove special characters from a string
sample_str = ''.join[item for item in sample_str if item.isalnum[]]

print[sample_str]

Output:

Test88String

It also removed all the spcecial characters from the string.

Remove special characters from a string using filter[]

In Python, we can use the filter[] function to filter out special characters from a string. Steps are as follows,

  • Along with the string to be modified, pass the isalpha[] function to the filter[] function, as the conditional argument.
  • filter[] function loops through all characters of string and yields only those characters for which isalpha[] function returns True i.e. all characters except the special characters.
  • Use join[] function to combine all yielded characters returned by filter[] function.
  • Assign back the joined string returned by join[] function to the original variable. It will give an effect that we have deleted all special characters from the string.

For example,

sample_str = "Test&[88]%%$$$#$%-+String"

# Remove special characters from a string
sample_str = ''.join[filter[str.isalnum, sample_str]]

print[sample_str]

Output:

Test88String

It also removed all the special characters from the string.

Remove special characters from a string using translate[]

The string class in python, has a function translate[]. It accepts a translation table as an argument and replaces the characters in string based on the mapping in the translation table. We can create a translation table, where each special character will be mapped to an empty string. For that we can use string.punctuation, that contains all the special characters and the maketrans[] function that creates a translation table.

We will pass this translation table to translate[] function as an argument. Due to which translate[] function will replace all the occurrences of special characters with an empty string. Basically it will remove all the occurrences of special characters from the string. For example,

import string

sample_str = "Test&[88]%%$$$#$%-+String"

# Create translation table in which special charcters
# are mapped to empty string
translation_table = str.maketrans['', '', string.punctuation]

# Remove special characters from the string using translation table
sample_str = sample_str.translate[translation_table]

print[sample_str]

Output:

Test88String

It also removed all the spcecial characters from the string.

Summary:

We learned about different ways to delete the spcecial characters from a string in python.

How do you remove unwanted text from a list in Python?

How to Remove an Element from a List Using the remove[] Method in Python. To remove an element from a list using the remove[] method, specify the value of that element and pass it as an argument to the method. remove[] will search the list to find it and remove it.

How do I remove special characters from a dataset in Python?

Remove Special Characters Including Strings Using Python isalnum. Python has a special string method, . isalnum[] , which returns True if the string is an alpha-numeric character, and returns False if it is not. We can use this, to loop over a string and append, to a new string, only alpha-numeric characters.

How do I remove unwanted symbols in Python?

Removing symbol from string using replace[] One can use str. replace[] inside a loop to check for a bad_char and then replace it with the empty string hence removing it.

Chủ Đề