Table of Contents
🎯 Objective: In this article, we will discuss the methods to remove ‘\n
’ or newline characters from a list.
Introduction
In a string, the ‘\n
’ represents newline. But having them in your list can be a problem if you want to extract a given string from a list with no escape characters. This means, if you print the elements of the list, then the ‘\n’ characters will also be printed.
Example: Suppose you have two lists and you want to print the corresponding elements of each list simultaneously.
1 2 3 4 5 6 7 8 |
lang = ['C\n','C++\n','Python\n','Java\n','R\n'] dev = ['Dennis Ritchie','Bjarne Stroustrup','Guido van Rossum','James Gosling','John Chambers'] print('Language - Developer') for num,i in enumerate(lang): print(i,"-",dev[num]) |
Expected Output:
Language – Developer
C – Dennis Ritchie
C++ – Bjarne Stroustrup
Python – Guido van Rossum
Java – James Gosling
R – John Chambers
Actual Output:
Language – Developer
C
– Dennis Ritchie
C++
– Bjarne Stroustrup
Python
– Guido van Rossum
Java
– James Gosling
R
– John Chambers
While printing the list lang
, the newline character is also taken into account by Python. That leads to the corresponding item
of the second list, i.e., dev
to be printed on the new line instead of being printed alongside the item of list lang
.
So, without further delay, let us dive into the solutions to visualize how you can remove the newline character from the list.
✨ Method 1: Using the replace() method
📌 replace()
is an inbuilt method in Python that is used to replace an old substring with a new one.
Syntax:
string.replace(Old_value, New_value ,Count) |
Here:
- Old_value: Means the old or present substring
- New_value: Means the new substring
- Count: It is an optional parameter. It represents the number of substrings you want to replace. If left blank, the method replaces all the occurrence of the old substring with the new one.
📌 Thus, you can use the replace()
method to replace all the ‘\n’
characters with the ‘ ’
whitespace character.
Solution
1 2 3 4 5 6 7 8 |
lang = ['C\n', 'C++\n', 'Python\n', 'Java\n', 'R\n'] dev = ['Dennis Ritchie', 'Bjarne Stroustrup', 'Guido van Rossum', 'James Gosling', 'John Chambers'] print('Language - Developer') for num, i in enumerate(lang): print(i.replace('\n', ''), "-", dev[num]) |
Output:
Language – Developer
C – Dennis Ritchie
C++ – Bjarne Stroustrup
Python – Guido van Rossum
Java – James Gosling
R – John Chambers
✨ Method 2: Using re.sub()
Another way to remove any newline characters from a list is to use regular expressions. You just need to import the re
module (Regular Expressions module) and use the method sub()
of this module.
📌 re.sub()
method returns a string, replacing any substring which matches the pattern (provided) with a new substring in that string.
Syntax:
s = re.sub(‘pattern’, ‘replacement’, str) |
Solution: Hence, using re.sub()
you can easily remove single as well as consecutive occurrences of newline characters with empty strings as shown below.
1 2 3 4 5 6 7 8 9 10 |
import re lang = ['C\n', 'C++\n', 'Python\n', 'Java\n', 'R\n'] dev = ['Dennis Ritchie', 'Bjarne Stroustrup', 'Guido van Rossum', 'James Gosling', 'John Chambers'] print('Language - Developer') for num, i in enumerate(lang): print(re.sub('\n','',i), "-", dev[num]) |
Output:
Language – Developer
C – Dennis Ritchie
C++ – Bjarne Stroustrup
Python – Guido van Rossum
Java – James Gosling
R – John Chambers
✨ Method 3: Using strip()
📌 strip()
is another built-in method in Python that removes specified characters from the beginning and end of a given string.
Syntax:
string.strip(characters) |
Solution: You can use the strip('\n')
method to remove the newline characters from the list elements as shown below.
1 2 3 4 5 6 7 8 |
lang = ['C\n', 'C++\n', 'Python\n', 'Java\n', 'R\n'] dev = ['Dennis Ritchie', 'Bjarne Stroustrup', 'Guido van Rossum', 'James Gosling', 'John Chambers'] print('Language - Developer') for num, i in enumerate(lang): print(i.strip('\n'), "-", dev[num]) |
Output:
Language – Developer
C – Dennis Ritchie
C++ – Bjarne Stroustrup
Python – Guido van Rossum
Java – James Gosling
R – John Chambers
✨ Method 4: Slice the List Elements
Slicing allows you to extract a substring from an entire string with the help of it’s index.
Example: The following code extracts all the characters from a string except the last character.
text = ‘ABCDE#’
print(text[:-1])
# OUTPUT: ABCDE
Thus, another work-around to our problem is to eliminate the newline characters is to slice the list elements and remove the ‘\n
‘ character from the end of the string as shown below.
1 2 3 4 5 6 7 8 9 10 |
import re lang = ['C\n', 'C++\n', 'Python\n', 'Java\n', 'R\n'] dev = ['Dennis Ritchie', 'Bjarne Stroustrup', 'Guido van Rossum', 'James Gosling', 'John Chambers'] print('Language - Developer') for num, i in enumerate(lang): print(i[:-1], "-", dev[num]) |
Output:
Language – Developer
C – Dennis Ritchie
C++ – Bjarne Stroustrup
Python – Guido van Rossum
Java – James Gosling
R – John Chambers
Explanation: We used i[:-1]
and this ensured that the last character of the item is not included. Since the last character is ‘\n
‘, hence we were able to eliminate it and print the proper output.
✨ Method 5: Using List Comprehension and Splitlines Function
Now, this is a tricky but effective way of eliminating ‘\n
‘ characters from the list.
- Use the
splitlines()
method to split the list element at line breaks i.e. “\n
” and then store them in another list using a list comprehension. - But
splitlines
will store the items in another list. Therefore, you will now have a list of list. So, you have to flatten this list using another list comprehension and print the desired result.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
lang = ['C\n', 'C++\n', 'Python\n', 'Java\n', 'R\n'] # using splitlines to split at line breaks li = [x.splitlines() for x in lang] print("li: ", li) # flatten the nested list li new_lang = [a for i in li for a in i] print("new_lang:", new_lang) dev = ['Dennis Ritchie', 'Bjarne Stroustrup', 'Guido van Rossum', 'James Gosling', 'John Chambers'] print() print('Language - Developer') for num, i in enumerate(new_lang): print(i, "-", dev[num]) |
Output:
li: [[‘C’], [‘C++’], [‘Python’], [‘Java’], [‘R’]]
new_lang: [‘C’, ‘C++’, ‘Python’, ‘Java’, ‘R’]
Language – Developer
C – Dennis Ritchie
C++ – Bjarne Stroustrup
Python – Guido van Rossum
Java – James Gosling
R – John Chambers
✨ Method 6: Using str.maketrans(x,y) and str.translate(map)
- Last but not least, we have
str.maketrans()
method. This method takes two substrings x and y of equal length and returns a translation table that maps each character in x to the character at the same position in y. - Thereafter, we need the
str.translate()
which returns a copy of the string where all characters have been mapped according to the dictionary returned bystr.maketrans()
.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 |
lang = ['C\n', 'C++\n', 'Python\n', 'Java\n', 'R\n'] for i in range(len(lang)): li = lang[i].maketrans('\n', ' ') lang[i] = lang[i].translate(li) dev = ['Dennis Ritchie', 'Bjarne Stroustrup', 'Guido van Rossum', 'James Gosling', 'John Chambers'] print() print('Language - Developer') for num, i in enumerate(lang): print(i, "-", dev[num]) |
Output:
Language – Developer
C – Dennis Ritchie
C++ – Bjarne Stroustrup
Python – Guido van Rossum
Java – James Gosling
R – John Chambers
⚠️ Caution:
- This might not be a suitable method in every scenario because we are replacing the newline character with a whitespace character in the above case while using the
maketrans
method. - Since, the presence of a whitespace does not hamper the final output in our code, hence this method deserves a mention in the list of our solutions.
Conclusion
Therefore, in this article, you learned about numerous methods to eliminate the newline character from a list element and I hope the above-mentioned concepts answered all your queries.
Read here: How To Print a Matrix in Python?
Please subscribe and stay tuned for more interesting tutorials in the future.
👨🎓 Author: Shubham Sayon
👨🎓 Co-Author: Anirban Chatterjee