Love maybe lines: ‘list’ object has no attribute ‘strip’

Hey there!

This is my first post here so I am sorry if something is wrong with formatting.
After a few things trying to get the solution I managed to get a step further with this code:

love_maybe_lines_stripped = []
for lines in love_maybe_lines:
  new_line = lines.strip()

Obviously I am happy it worked but I wanted to ask why this code

love_maybe_lines_stripped = [love_maybe_lines.strip() for lines in

won’t work as it says that: ‘list’ object has no attribute ‘strip’.

I get that you cannot use the .strip() method for lists per se but isn’t the for loop method used in a list comprehension taking the strings from the list one by one to generate the new list and so it should be handled as a string?

Thank you in advance, I really enjoy the course and your help here in the forum, it helps a lot!


EDIT: readability second code

1 Like

In your list comprehension you iterating through love_maybe_lines and calling the result lines each time. What are you performing the strip method on?

The strip method was supposed to be performing for every line from the love_maybe_lines list.

love_maybe_lines_stripped = [love_maybe_lines.strip() for lines in

For clarification, why I am asking this, Prior to this exercise one solution for splitting the authors names was:

last_names = [authors.split()[-1] for authors in author_names]

that’s why I was thinking it would be possible here also.

(As I am right know wondering that ‘authors’ was the original string and not a list which in my case love_maybe_lines is and so the split.() method is working?)

I am confused haha. Is there a list comprehension possible for this .strip() exercise?

EDIT: highlights not possible in formatted code. oopsie^^

What would happen if you performed the following code outside the list comprehension?


It would throw an error as love_maybe_lines is a list. In the example you gave, is split being performed on the list author_names? What’s different?

Apologies for being a little obtuse I was trying to get you to hunt the error down yourself. It may just be a little confusion from using list comprehensions for the first time. If the syntax is troubling at first try writing the code out in a standard for loop just for a quick sanity check.

1 Like

Yeah you are right I got confused with the .split() or .strip() methods generating a list and switched up the original string with the generated list.
Definitely have to check that example up again and just try things.

Thanks a lot! (I don’t mind your approach, it really is a good way to figure out the fault by oneself)

1 Like