My list has every letter seperated instead of each line?

Hello!
Doing the exercise, I wrote this code:

Which I don’t understand because it gives me back that result, letter by letter.

Greetings

Did you expect something else? Have you thought through what you expected? That may tell you a lot about what to change. Even if you don’t know how to make it happen, deciding what should happen is a very good start and arguably required since you, the programmer, is the one deciding how information should be moving around in your program (that’s not something to be left to chance)

If you’ve got each stripped line as individual strings, and you then end up with a list of individual characters with no linefeeds, then I think that is good enough information to conclude that the issue is with how those strings were added to the list - what list operation did you use, what did you ask the list to do? Maybe list makes some other operation(s) available to you that suit you better.

1 Like

Thank you very much for your reply.
I perfectly understand what you are saying. But my question went to the most technical.
I’m using a blucle for and taking each string from the list and removing the spaces with .sprit().
What I hoped would happen is that I would return the entire string without the spaces removed, just as the next for loop would execute:

for line in love_maybe_lines:
love_maybe_lines_stripped.append(line.strip())

What I don’t understand is why you don’t do the same with (+=).
Maybe my question is very silly, but I’m just beginning and it would be useful to explain why I don’t get the same result in the loop when I use (+=) and not .append().
It is very possible that I misunderstood the concept of (+=) and misapplied it.

When concatenating to a list, both objects must be lists.

+= [line.strip()]
2 Likes

What do you have to support that += should behave like append?
Right? It does something, and it’s not going to do what you want, it’s going to do whatever it was defined to do.

One reason why += shouldn’t behave like you want it to is that + doesn’t behave like that:

[] + 5  # uhm, what?

Instead, list’s + expects another list:

[] + []  # still empty, but this makes sense at least

You might then expect += to raise some error when the other value isn’t a list, same as + would. However, it iterates over the other value, it doesn’t care at all what type it is so long as it is iterable. And if you iterate over a string you get individual characters.

2 Likes

Now I understand.
Thank you so much!

1 Like

I didn’t know that.
Your contributions helped me a lot
Thank you so much!