How to Remove \N from a List Element

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



Leave a reply



Submit