Remove leading whitespace from each string in List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove leading whitespace from each string in List in Python #

To remove the leading whitespace from each string in a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the lstrip() method to remove the leading whitespace from each string.
  3. The elements in the new list won't contain leading whitespace.
main.py
my_list = [' apple ', ' c a t', ' dog '] # ✅ Remove leading spaces from list of strings new_list = [item.lstrip() for item in my_list] print(new_list) # 👉️ ['apple ', 'c a t', 'dog '] # ------------------------------------------- # ✅ Remove trailing spaces from list of strings new_list = [item.rstrip() for item in my_list] print(new_list) # 👉️ [' apple', ' c a t', ' dog'] # ------------------------------------------- # ✅ Remove leading and trailing spaces from list of strings new_list = [item.strip() for item in my_list] print(new_list) # 👉️ ['apple', 'c a t', 'dog'] # ------------------------------------------- # ✅ Remove all spaces from list of strings new_list = [item.replace(' ', '') for item in my_list] print(new_list) # 👉️ ['apple', 'cat', 'dog']

The first example uses a list comprehension to remove the leading 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 = [' apple ', ' c a t', ' dog '] new_list = [item.lstrip() for item in my_list] print(new_list) # 👉️ ['apple ', 'c a t', 'dog ']

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

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

If you need to remove the leading and trailing whitespace from each string, use the str.strip() method.

main.py
my_list = [' apple ', ' c a t', ' dog '] # ✅ Remove leading and trailing spaces from list of strings new_list = [item.strip() for item in my_list] print(new_list) # 👉️ ['apple', 'c a t', 'dog']

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 = [' apple ', ' c a t', ' dog '] # ✅ Remove all spaces from list of strings new_list = [item.replace(' ', '') for item in my_list] print(new_list) # 👉️ ['apple', 'cat', 'dog']

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.

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.