How to remove \n from a list element?
If you want to remove \n
from the last element only, use this:
t[-1] = t[-1].strip()
If you want to remove \n
from all the elements, use this:
t = map(lambda s: s.strip(), t)
You might also consider removing \n
before splitting the line:
line = line.strip()
# split line...
The most efficient way to remove first N elements in a list?
You can use list slicing to archive your goal.
Remove the first 5 elements:
n = 5
mylist = [1,2,3,4,5,6,7,8,9]
newlist = mylist[n:]
print newlist
Outputs:
[6, 7, 8, 9]
Or del
if you only want to use one list:
n = 5
mylist = [1,2,3,4,5,6,7,8,9]
del mylist[:n]
print mylist
Outputs:
[6, 7, 8, 9]
How to delete '\n' from items of a list
You can try this
bodylist = df.values.tolist()
bodylist = [elt.replace("\n", "") for elt in bodylist]
Given you have a list, you browse it with comprehension and create a new list without the «\n» character.
How to remove \n in every Arraylist item
Change
item.replaceAll("\n", "");
to
idList.set(i,item.replaceAll("\n", ""));
item.replaceAll
doesn't modify the state of the String
referenced by item
(which is impossible, since String
is immutable). It returns a new String
instead.
Is there a way to remove "\n" in a list
Use .replace()
method
string_list = [
"D:/Music/Song.mp3\n",
"hello world\n"
]
new_string_list = [string.replace("\n","") for string in string_list]
Related Topics
Print() Prints Only Every Second Input
Replacing All Negative Values in Certain Columns by Another Value in Pandas
How to Add Parenthesis Around a Substring in a String
Using Regex to Get the Value Between Two Characters (Python 3)
Passing Multiple Arguments from Django Template Href Link to View
How to Remove Name and Dtype from Pandas Output
Pandas Counting and Summing Specific Conditions
How to Save a Pandas Dataframe Table as a Png
How to Find 3 Immediate Words After Keyword Match Using Python
Calculating the Area Under a Curve Given a Set of Coordinates, Without Knowing the Function
Decode Utf-8 Encoding in Json String
Pandas Filtering for Multiple Substrings in Series
How to Extract Hours and Minutes from a Datetime.Datetime Object
Python Pandas - Get Row Based on Previous Row Value
Macos: How to Downgrade Homebrew Python
Python + Beautifulsoup: How to Get 'Href' Attribute of 'A' Element