Home » Python » Remove a Character from List in Python

Remove a Character from List in Python

To remove a character from a list of strings in Python, use the str.replace() method.

str.replace() method in Python replaces the specified phrase in the string with the new phrase. Use the replace() method to remove all instances of a character from a list of strings. For example, replacing all instances of character ‘a’ with ”

In this example, we will discuss how to use the str.replace() method in Python to remove a character from a list of strings.

Use str.replace() to remove a character from a list of strings

To remove a character from a list in Python:

  1. Call the str.replace() method on the list of strings and pass a character to search for a replacement and a new character for a replacement.
  2. The replace() method returns a list of strings in which all occurrences of a character are replaced by a new character.
list_of_strings = ["Python","Program"]

updated_list = [str.replace("o","") for str in list_of_strings]
print(updated_list)

In the above Python program, we pass the following parameters to replace() method:

  1. list_of_strings variable stores the list of strings
  2. Use the str.replace() method with [str for str in list]
  3. character ‘o’ to search for in the string.
  4. replacement character ”. It will replace all occurrences of ‘o’ with ”

It will return the copy of the list of strings and store it in the updated_list variable and print it using the print() method in Python.

The output of the above program after removing a character from a list of strings is:

['Pythn', 'Prgram']

Cool Tip: How to remove all occurrences of a character in a string Python!

Conclusion

I hope the above article on how to remove a character from a list of strings in the Python using str.replace() method is helpful to you.

You can find more topics about the Python tutorials on the DataVisualizr Home page.

Leave a Comment