Remove newline characters from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 21, 2022

banner

Photo from Unsplash

Remove newline characters from a List in Python #

To remove the newline characters from a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.strip() method to remove the leading and trailing newlines from each string.
  3. The new list won't contain leading and trailing newlines.
main.py
my_list = ['one\n', 'two\r\n', '\nthree\r\n'] # ✅ Remove leading and trailing newline characters from list (str.strip()) new_list = [item.strip() for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three'] # --------------------------------------------- # ✅ Remove only trailing newline characters from items in list (str.rstrip()) new_list = [item.rstrip() for item in my_list] print(new_list) # 👉️ ['one', 'two', '\nthree'] # --------------------------------------------- # ✅ Remove only leading newline characters from items in list (str.lstrip()) new_list = [item.lstrip() for item in my_list] print(new_list) # 👉️ ['one\n', 'two\r\n', 'three\r\n'] # --------------------------------------------- # ✅ Remove ALL newline characters from list (str.replace()) new_list = [item.replace('\r', '').replace('\n', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

The examples use a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

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

main.py
my_list = ['one\n', 'two\r\n', '\nthree\r\n'] new_list = [item.strip() for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

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

The strings in the new list won't contain any leading and trailing newline characters.

The list comprehension doesn't mutate the original list, it returns a new list.

If you need to mutate the original list, use a slice.

main.py
my_list = ['one\n', 'two\r\n', '\nthree\r\n'] my_list[:] = [item.strip() for item in my_list] print(my_list) # 👉️ ['one', 'two', 'three']

We used the my_list[:] syntax to get a slice that represents the entire list, so we can assign to the variable directly.

The slice my_list[:] represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.

This approach changes the contents of the original list.

If you only need to remove the leading or trailing newline characters from each string in a list, use the str.lstrip() or str.rstrip() methods.

main.py
my_list = ['one\n', 'two\r\n', '\nthree\r\n'] new_list = [item.rstrip() for item in my_list] print(new_list) # 👉️ ['one', 'two', '\nthree'] new_list = [item.lstrip() for item in my_list] print(new_list) # 👉️ ['one\n', 'two\r\n', 'three\r\n']

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

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

Alternatively, you can use the str.replace() method.

Remove newline characters from a List using str.replace() #

To remove the newline characters from a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.replace() method to remove all newline characters from each string.
  3. The items in the new list won't contain any newline characters.
main.py
my_list = ['one\n', 'two\r\n', '\nthree\r\n'] new_list = [item.replace('\r', '').replace('\n', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

We used the str.replace() method to remove the newline characters from each string in the list.

This approach removes all newline characters from each string in the list. Not only the leading and trailing ones.

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 provided an empty string as the replacement because we want to remove all newline characters.

Notice that we called the str.replace() method 2 times.

This is necessary because the newline characters differ between operating systems.

Windows uses \r\n as an end of line character, whereas \n is the default in Unix.

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.