Home > Net >  How to remove the newline characters from each word in a list?
How to remove the newline characters from each word in a list?

Time:10-30

How can I remove the first two characters of a word that is in a list?

For example, I have the following list:

List = ['\ nNew', '\ nIndustrial', '\ nColombia']

How can I remove the '\ n' from each word?

CodePudding user response:

l = ['\ nNew', '\ nIndustrial', '\ nColombia']
clean_list = [ w[3:] for w in l ]

Basically two things here :

  • Used list comprehension to create a new list
  • Used string slicing to remove the first three characters

CodePudding user response:

In javascript, you can use map to iterate through the list & use slice to remove the first few characters. See code below:

List = ['\ nNew', '\ nIndustrial', '\ nColombia'];

newList = List.map(item => item.slice(2,));

console.log(newList);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Something simple like

for (String str : list) {
    str.replace("\n", "");
}

would work if this is in Java. Or even list.forEach(str -> str.replace("\n", ""));.

  • Related