Hey, welcome back.
In this video, we will make our program a bit more user friendly because currently we get these break
lines here, which it's not so good to have from the user perspective.
So let's remove these break lines.
But how do we go about this?
Again, introduce the text.
We don't have these break lines.
Actually, we have break lines, but only one break line.
So there is a backslash n after prepare and then there is another backslash n after shower and so on.
So the extra break line is being produced by the print function here.
So we end up with two sets of break lines for each line.
So how do we go about this?
Do we remove the brick lines from the text file or we do something in here?
Well.
Of course, we shouldn't edit the text file.
We should edit the representation of the data that is in the text file.
So the representation is the list.
Therefore we should add it to the list.
So that's the case.
BLOCK And here we get.
The to do list from the text file and then we iterate over that to do list.
But now a solution we could think about is to remove.
The backslash and characters from the list.
Because currently, if we print out the list here, we execute the program again, we press show, and
this time you're going to see that that's the print function that I've just added.
So that print function produces that list.
So the list has these backslash characters.
And then the loop add some more.
So let's remove this back slashes and then we fix the problem.
So we remove the backslash from each item.
Therefore, the loop will only produce one backslash.
The backslash that it produces by default and it will not have any other backlashes.
Therefore, how do we add it?
Each of these items.
So how do we perform batch operations?
You learned four loops earlier in the course, so that is something you can use.
There are two solutions for this actually.
One is for loops and one is list compressions.
And we're going to go through both.
So let's first start with for loops.
That's the existing list.
But what I'll do is I'll create a new list here.
So in the same indentation level, let's say new to dos variable is equal to an empty list.
And then what we do is.
We iterates over four item in to dos.
We iterate over to dos so our item will be each item of the to do list, including the backslash n.
And then.
We say new.
It will be equal to item that strip.
Backslash.
And so that's command.
What it does is, is this.
So let's say item will be equal to.
Prepare.
Backslash n.
Therefore new item is equal to item strip.
Backslash.
NW So a new item now is going to be equal to just prepare without the backslash N while item is that
one so new item will be without the backslash.
And then what we do is we access new to DOS that append and we add new item there.
So we're going to end up with a new list which is going to contain the items without the backslash and
characters.
Then what we could do is in here we iterate over new to dos instead of.
To reduce.
And that's with solve the issue.
So now let me rerun the script.
And type show.
And these are the two do's.
We also get the lists here from that print so we can remove that print function.
We don't need it anymore.
So that is one way to do it.
Another way to clean up the list is to use a list comprehension.
A list comprehension is the equivalent of a full loop.
So it's just another way.
To modify the items of the list, but it's a shorter way.
You'll see why and list comprehension.
All it takes is one single line, and it starts always with square brackets.
So inside here you write an inline four loop, so a four loop in one line you would say item that strip.
Backslash n.
So that is one expression.
And you say for item in.
To dos.
And this is going to be new to dos.
So this is basically a list.
I am going to remove these.
We don't need them anymore.
But before I remove them, just try to compare the full loop with this.
So basically what we're saying is that you can start reading this from here to understand it.
So for Autumn, introduce.
You iterates over the to do list store item that strip backslash and in this list.
So this will return an item without the backslash and so this item will be part of the list and this
is repeated over the other items.
So we end up with three items.
As part of this list.
So this will be just a list.
So let me delete that now.
And the declaration of the new to do list.
So with the list comprehension, you don't even have to declare an empty list.
You create the list on the fly using the list comprehension.
So run.
Show.
And we get the same exact output using a list comprehension.
However, perhaps we didn't even need to use a list comprehension here.
We could apply a more direct method to remove the backslash and characters from.
These to do items.
So that way would be to delete this.
The way is to iterate over to DOS as it was.
So this was the code in the beginning of the video which produces again.
These items with extra brake lines.
And then what you could do is.
You want to override the item variable?
You could do something like item is equal to item strip.
And then here you place backslash n.
And then this item here will be the new string without the backslash n character.
So let me try this solution as well.
And here we go.
So I suppose this was a simple solution, but.
This was a good opportunity for you to understand.
List comprehension.
So whenever you want to modify.
The items of the list.
You want to use a list comprehension.
And of course, luckily or unluckily, sometimes there are more than one solution to do something.
Which one is the best?
Well, it depends.
You should think about readability, but also.
Efficiency.
So how code, how fast your code runs.
And there are ways to measure the speed of your code.
And we'll cover that later.
For now, it is important to learn all the core elements of the language.
So thank you for following this.
I'll talk to you in the next videos.