Remove spaces from a list of strings in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove spaces from a list of strings in Python #

To remove the spaces from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Use the strip() method to remove the leading and trailing whitespace from each string.
  3. The elements in the new list won't contain leading or trailing whitespace.
main.py
my_list = [' one ', ' t w o', 'three ', ' four ', 'f i v e'] # ✅ Remove leading and trailing spaces from list of strings new_list = [item.strip() for item in my_list] print(new_list) # 👉️ ['one', 't w o', 'three', 'four', 'f i v e'] # ----------------------------------------------------- # ✅ Remove all spaces from list of strings new_list = [item.replace(' ', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three', 'four', 'five'] # ----------------------------------------------------- # ✅ Remove leading spaces from list of strings new_list = [item.lstrip() for item in my_list] print(new_list) # 👉️ ['one ', 't w o', 'three ', 'four ', 'f i v e'] # ----------------------------------------------------- # ✅ Remove trailing spaces from list of strings new_list = [item.rstrip() for item in my_list] print(new_list) # 👉️ [' one', ' t w o', 'three', ' four', 'f i v e']

The first example uses a list comprehension to remove the leading and trailing whitespace from each string in a list of strings.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
my_list = [' one ', ' t w o', 'three ', ' four ', 'f i v e'] new_list = [item.strip() for item in my_list] print(new_list) # 👉️ ['one', 't w o', 'three', 'four', 'f i v e']

On each iteration, we use the str.strip() method to remove the leading and trailing whitespace from the string.

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

If you need to remove all spaces from each string in the list, use the str.replace() method.

main.py
my_list = [' one ', ' t w o', 'three ', ' four ', 'f i v e'] # ✅ Remove all spaces from list of strings new_list = [item.replace(' ', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three', 'four', 'five']

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)
We specified an empty string for the replacement string because we want to remove all occurrences of a space in the string.

The method doesn't change the original string. Strings are immutable in Python.

If you only need to remove the leading whitespace from each string in the list, use the str.lstrip() method.

main.py
my_list = [' one ', ' t w o', 'three ', ' four ', 'f i v e'] # ✅ Remove leading spaces from list of strings new_list = [item.lstrip() for item in my_list] print(new_list) # 👉️ ['one ', 't w o', 'three ', 'four ', 'f i v e']

The str.lstrip method returns a copy of the string with the leading whitespace removed.

If you need to remove the trailing whitespace from each string in the list, use the str.rstrip() method.

main.py
my_list = [' one ', ' t w o', 'three ', ' four ', 'f i v e'] # ✅ Remove trailing spaces from list of strings new_list = [item.rstrip() for item in my_list] print(new_list) # 👉️ [' one', ' t w o', 'three', ' four', 'f i v e']

The str.rstrip method returns a copy of the string with the trailing whitespace removed.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.