Here we will learn 8 different ways to remove a newline character from a list in python? In Python, there are many built-in functions are available to remove a newline character from the list. We tried to cover all the possible methods here. This article is going to be very interesting and helpful to learn.
Newline character is specified using a special character “\n”. It is useful to generate a new line. Whenever we are using a character \n, it will automatically generate a new line. The following methods are available to remove a newline are:
- slice operator
- replace() method
- re.sub() function
- strip function
- rstrip() function
- map function with lambda
- map function without lambda
- enumerate
These are the methods available to remove the newline characters from the list.
Method 1 : Using slicing operator to remove newline in python
The slice operator is useful to represent the index of the character. Slicing has two methods positive indexing and negative indexing. Positive indexing starts with position value [0] and negative indexing starts with [-1]. Slicing is useful to specify where to start or end the list.
Syntax
list[initial : end : jump value]
Parameter
- initial – initial value of the list
- end – final value of the list
- jump value – increment values
Return
sliced value
Code
lst=['This\n','is\n','Python\n','Pool\n']
new_lst = [x[:-1] for x in lst]
print(new_lst)
A variable lst holds the list to remove a new line character. Using negative slicing to remove the \n character. Here the new line character is at the end, so we are using negative indexing. For loop is useful to iterate till the end of the list.
Output
['This', 'is', 'Python', 'Pool']
Recommended Reading | Python Print Without Newline [How to, Examples, Tutorial]
Method 2 : Using replace method to remove newline in python
This replace() method is useful to replace the specified character with a given character. It is a built-in function in python. So we need not install this function separately.
Syntax
list.replace(old value, new value, count)
Parameters
- old value – original value in a list or string
- new value – the list to be replaced
- count – specifying how many times the list is to be replaced, optional.
Returns
replaced list
Code
lst=['This\n','is\n','Python\n','Pool\n']
rep = []
for x in lst:
rep.append(x.replace("\n", ""))
print(list(rep))
A variable lst holds the list to remove a new line character. Declaring an empty list in a name of rep(). Creating for loop to iterate till the end of the list. After removing the newline character it will store in a rep variable. For that, we are using the append() function to add at the end of the list.
Output
['This', 'is', 'Python', 'Pool']
Method 3: Using re.sub() function to remove newline in python
This function is also similar replace() function. re.sub() is also useful to replace the specified character with a given character. It is a built-in function in python. So we need not install this function separately.
Syntax
re.sub(pattern,repl,string).
Parameters
- pattern
- repl
- string
Returns
replaced list
Code
import re
lst=['This\n','is\n','Python\n','Pool\n']
rep = []
for x in lst:
rep.append(x.replace("\n", ""))
print(list(rep))
Importing re module. A variable lst holds the list to remove a new line character. Declaring an empty list in a name of rep(). Creating for loop to iterate till the end of the list. After removing the newline character it will store in a rep variable. For that, we are using the append() function to add at the end of the list.
Output
['This', 'is', 'Python', 'Pool']
Recommended Reading | Multiple Ways To Print Blank Line in Python
Method 4 : Using strip() function to remove newline in python
strip() function is useful to remove white spaces or any special characters. It is a built-in function in python.
Syntax
list.strip(characters)
Parameter
characters – optional
Returns
list without any special characters
Code
lst=['This\n','is\n','Python\n','Pool\n']
new_lst=[]
for i in lst:
new_lst.append(i.strip())
print(new_lst)
A variable lst holds the list to remove a new line character. Declaring an empty list in a name of new_lst. Creating for loop to iterate till the end of the list. After removing the newline character it will store in a new_lst variable. For that, we are using the append() function to add at the end of the list. Using strip function to remove the newline character.
Output
['This', 'is', 'Python', 'Pool']
Method 5 : Using rstrip function to remove newline in python
This function is also similar strip() function. rstrip() function is useful to remove white spaces or any special characters. It is a built-in function in python.
Syntax
list.rstrip(characters)
Parameter
character
Return
A string
Code
lst=['This\n','is\n','Python\n','Pool\n']
new_lst=[]
for i in lst:
new_lst.append(i.rstrip())
print(new_lst)
A variable lst holds the list to remove a new line character. Declaring an empty list in a name of new_lst. Creating for loop to iterate till the end of the list. After removing the newline character it will store in a new_lst variable. For that, we are using the append() function to add at the end of the list. Using the rstrip function to remove the newline character.
Output
['This', 'is', 'Python', 'Pool']
Method 6 : Using map function with the lambda
map function is a built-in function. It is useful to process and transform all the items in an iterable. The map() function takes a function and a list as an argument.
Syntax
lambda arguments: expression
Parameter
expression
Code
lst =['This\n','is\n','Python\n','Pool\n']
print(list(map(lambda x:x.strip(),lst)))
A variable lst holds the list to remove a new line character. Using map function with lambda to remove the newline character. Here strip() function is also useful to perform the task.
Output
['This', 'is', 'Python', 'Pool']
Method 7 : Using map function without lambda
lst= ['This\n','is\n','Python\n','Pool\n']
print(list(map(str.strip,lst)))
A variable lst holds the list to remove a new line character. Using map function without lambda to remove the newline character. Here strip() function is also useful to perform the task.
Output
['This', 'is', 'Python', 'Pool']
Method 8 : Using enumerate to remove a newline from a list
It is a built-in function that takes a collection and returns an enumerated object.
Syntax
enumerate(iterable, start)
Parameter
- iterable
- start
Return
enumerated object
Code
lst =['This\n','is\n','Python\n','Pool\n']
for i,n in enumerate(lst):
lst[i] = n.strip()
print(lst)
A variable lst holds the list. Creating for loop to iterate till the end of the loop. Using strip() to remove the new line character.
Output
['This', 'is', 'Python', 'Pool']
Frequently asked questions related to remove newline from list
“\n” is the newline character in python.
strip() function is useful to remove the spaces at the beginning and the end of the string.
Conclusion
Here we have learned 8 ways to remove a newline from the list in python. “\n” is a newline character in python. We hope this article is easy to understand. The list is enclosed within a square bracket and separated by commas.