Remove trailing newline from the elements of a string list

Question:

I have to take a large list of words in the form:

['thisn', 'isn', 'an', 'listn', 'ofn', 'wordsn']

and then using the strip function, turn it into:

['this', 'is', 'a', 'list', 'of', 'words']

I thought that what I had written would work, but I keep getting an error saying:

“‘list’ object has no attribute ‘strip'”

Here is the code that I tried:

strip_list = []
for lengths in range(1,20):
    strip_list.append(0) #longest word in the text file is 20 characters long
for a in lines:
    strip_list.append(lines[a].strip())
Asked By: George Burrows

||

Answers:

You can either use a list comprehension

my_list = ['thisn', 'isn', 'an', 'listn', 'ofn', 'wordsn']
stripped = [s.strip() for s in my_list]

or alternatively use map():

stripped = list(map(str.strip, my_list))

In Python 2, map() directly returned a list, so you didn’t need the call to list. In Python 3, the list comprehension is more concise and generally considered more idiomatic.

Answered By: Sven Marnach

list comprehension?
[x.strip() for x in lst]

Answered By: yosukesabai

You can use lists comprehensions:

strip_list = [item.strip() for item in lines]

Or the map function:

# with a lambda
strip_list = map(lambda it: it.strip(), lines)

# without a lambda
strip_list = map(str.strip, lines)
Answered By: Schnouki

This can be done using list comprehensions as defined in PEP 202

[w.strip() for w in  ['thisn', 'isn', 'an', 'listn', 'ofn', 'wordsn']]
Answered By: Casey

All other answers, and mainly about list comprehension, are great. But just to explain your error:

strip_list = []
for lengths in range(1,20):
    strip_list.append(0) #longest word in the text file is 20 characters long
for a in lines:
    strip_list.append(lines[a].strip())

a is a member of your list, not an index. What you could write is this:

[...]
for a in lines:
    strip_list.append(a.strip())

Another important comment: you can create an empty list this way:

strip_list = [0] * 20

But this is not so useful, as .append appends stuff to your list. In your case, it’s not useful to create a list with defaut values, as you’ll build it item per item when appending stripped strings.

So your code should be like:

strip_list = []
for a in lines:
    strip_list.append(a.strip())

But, for sure, the best one is this one, as this is exactly the same thing:

stripped = [line.strip() for line in lines]

In case you have something more complicated than just a .strip, put this in a function, and do the same. That’s the most readable way to work with lists.

Answered By: Joël

If you need to remove just trailing whitespace, you could use str.rstrip(), which should be slightly more efficient than str.strip():

>>> lst = ['thisn', 'isn', 'an', 'listn', 'ofn', 'wordsn']
>>> [x.rstrip() for x in lst]
['this', 'is', 'a', 'list', 'of', 'words']
>>> list(map(str.rstrip, lst))
['this', 'is', 'a', 'list', 'of', 'words']
Answered By: Eugene Yarmash
my_list = ['thisn', 'isn', 'an', 'listn', 'ofn', 'wordsn']
print([l.strip() for l in my_list])

Output:

['this', 'is', 'a', 'list', 'of', 'words']
Answered By: Vignesh Kini K

You can loop through your list and simply replace n with empty space "" without appending it to the new list

main_list = ['thisn', 'isn', 'an', 'listn', 'ofn', 'wordsn']

for i in range(0, len(main_list)):
    main_list[i] = main_list[i].replace("n", "")

Using this method you can perform multilple replacements at the same time.
Replacing n with empty space "" and replacing sample_text string with the "" for each element of the main_list

main_list = ['thisnsample_text', 'isnsample_text', 'ansample_text', 'listnsample_text', 'ofnsample_text', 'wordsnsample_text']

for i in range(0, len(main_list)):
    main_list[i] = main_list[i].replace("n", "").replace("sample_text", "")
Answered By: Andrew T.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.