How To Remove Special Characters From A List Of Strings In Python

To remove special characters from a List of Strings in Python, you can use the re.findall(), isalnum(), translate(), or replace() functions. Let’s see how to perform them.

Remove Special Characters From A List Of Strings In Python

Using the re.findall() function

You can use the findall() function in the ‘re’ module to remove special characters from a List of Strings in Python. This function will find the elements in the String that match the pattern.

Look at the example below.

import re

def remove_special_characters(str_list=list()):
    result = []

    # Iterate the current list of Strings.
    for item in str_list:
        # Find all characters that are words.
        arr = re.findall('\w+', item)

        # Convert the array to a String.
        new_string = ''.join(arr)

        # Add this String to the new list.
        result.append(new_string)
    return result

# The list to be used.
str_list = ['Le@!#arn', 'Sh%!a*re', 'I$$T']
print('The current list: {}'.format(str_list))

# Remove special characters from a List of Strings with the re.findall() function.
new_list = remove_special_characters(str_list)
print('The new list: {}'.format(new_list))

Output

The current list: ['Le@!#arn', 'Sh%!a*re', 'I$$T']
The new list: ['Learn', 'Share', 'IT']

Moreover, you can also use the sub() or the split() functions in the ‘re’ module to achieve your goals. Their functions of them are different from the findall() function but in this case, you can perform quietly the same way as it.

Using the translate() function

Besides the re.findall() function, you can use the translate() function to remove special characters from a List of Strings. Unlike the re.findall() function, you need to create the sequence of characters to be removed by yourself.

Look at the example below.

def remove_special_characters(str_list=list()):
    # Create the sequence of characters to be removed.
    remove_table = str.maketrans('', '', '@!#*$%')

    # Combine the translate() function with the list comprehension.
    result = [item.translate(remove_table) for item in str_list]
    return result

# The list to be used.
str_list = ['Le@!#arn', 'Sh%!a*re', 'I$$T']
print('The current list: {}'.format(str_list))

# Remove special characters from a List of Strings with the translate() function.
new_list = remove_special_characters(str_list)
print('The new list: {}'.format(new_list))

Output

The current list: ['Le@!#arn', 'Sh%!a*re', 'I$$T']
The new list: ['Learn', 'Share', 'IT']

Using the isalnum() function

In addition, you can use the isalnum() function to remove special characters from a List of Strings. This function is used to keep the normal characters from the String.

Look at the example below.

def remove_special_characters(str_list=list()):
    result = []

    # Iterate the list of Strings.
    for item in str_list:
        # Using the isalnum() function to keep the normal characters.
        item = ''.join(character for character in item if character.isalnum())

        # Add this String to the new list.
        result.append(item)
    return result

# The list to be used.
str_list = ['Le@!#arn', 'Sh%!a*re', 'I$$T']
print('The current list: {}'.format(str_list))

# Remove special characters from a List of Strings with the isalnum() function.
new_list = remove_special_characters(str_list)
print('The new list: {}'.format(new_list))

Output

The current list: ['Le@!#arn', 'Sh%!a*re', 'I$$T']
The new list: ['Learn', 'Share', 'IT']

Summary

We have shown you how to remove special characters from a List of strings in Python in 3 ways. Personally, you should use the re.findall() function in the ‘re’ module because you can customize it to find a String that matches the pattern, not only special characters. Hope you have an enjoyable learning experience. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *