Remove Special Characters From list Python

Here, we will develop a program to remove special characters from a list in python. If the list was [‘[email protected]’, ‘Python*Program’] then the result in the string will be [‘KnowProgram’, ‘PythonProgram’]. We will discuss how to remove all special characters from the given list using Regular Expression, translate(), join(), filter() method, and str.isalnum() function.

Python Remove Special characters from List

We will first be importing Regular Expression (RegEx module). The regular expression will automatically remove the special characters from the list. The regular expression for this will be [^a-zA-Z0-9], where ^ represents any character except the characters in the brackets.

# Python program to remove all special characters from list

# importing RegEx module
import re

# take list
my_list = ['@know*', 'pr#ogra!m^', '([email protected]_3}']
 
# using regular expression to remove special characters
out_list = [re.sub(r'[^a-zA-Z0-9]','',string) for string in my_list]

# print list without special characters
print('List after removal of special characters:', out_list)

Output:-

List after removal of special characters: [‘know’, ‘program’, ‘python3’]

How to Remove Special Characters From list elements in Python

We are using the join() method to remove special characters. In the generator function, we specify the logic to ignore the characters in special_char and hence constructing out_list free from special characters.

# Python program to remove all special characters from list

# take list
my_list = ['@know*', 'pr#ogra!m^', '([email protected]_3}']

# initializing special characters
special_char = '@_!#$%^&*()<>?/\|}{~:;.[]'
 
# using join() + generator to remove special characters
out_list = [''.join(x for x in string if not x in special_char) for string in my_list]
 
# print list without special characters
print('List after removal of special characters:', out_list)

Output:-

List after removal of special characters: [‘know’, ‘program’, ‘python3’]

Remove Special Characters from list Python

In the previous program, we used the join() method but in this program, we are using the join(), filter(), and lambda() function to remove all special characters from the list. The filter() method constructs an iterator from elements of an iterable for which a function returns true.

# Python program to remove all special characters from list

# take list
my_list = ['@know*', 'pr#ogra!m^', '([email protected]_3}']

# initializing special characters
special_char = '@_!#$%^&*()<>?/\|}{~:;.[]'
 
# using filter() to remove special characters
out_list = [''.join(filter(lambda i: i not in special_char, string)) for string in my_list]
 
# print list without special characters
print('List after removal of special characters:', out_list)

Output:-

List after removal of special characters: [‘know’, ‘program’, ‘python3’]

Remove all Special Characters

This python program also performs the same task but in different ways. In this program, we are also using str.isalnum() function. The str.isalnum() method returns True if the characters are alphanumeric characters, meaning no special characters in the string. It will return False if there are any special characters in the string.

# Python program to remove all special characters from list

# take list
my_list = ['@know*', 'pr#ogra!m^', '([email protected]_3}']
 
# using filter() to remove special characters
out_list = [''.join(filter(str.isalnum, string)) for string in my_list]
 
# print list without special characters
print('List after removal of special characters:', out_list)

Output:-

List after removal of special characters: [‘know’, ‘program’, ‘python3’]

Replace Special Characters in the list

The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table. Use the maketrans() method to create a mapping table. If a character is not specified in the dictionary/table, the character will not be replaced. We can translate each special_char to an empty string and get the filtered string.

# Python program to remove all special characters from list

# importing string function
import string

# take list
my_list = ['@know*', 'pr#ogra!m^', '([email protected]_3}']
 
# using translate() to remove special characters
removetable = str.maketrans('', '', string.punctuation)
out_list = [s.translate(removetable) for s in my_list]

# print list without special characters
print('List after removal of special characters:', out_list)

Output:-

List after removal of special characters: [‘know’, ‘program’, ‘python3’]

Also See:- Check if String Starts with Vowel

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply