1
00:00:01,900 --> 00:00:02,860
Hey, welcome back.

2
00:00:02,890 --> 00:00:09,100
In this video, we will make our program a bit more user friendly because currently we get these break

3
00:00:09,100 --> 00:00:15,640
lines here, which it's not so good to have from the user perspective.

4
00:00:15,670 --> 00:00:18,250
So let's remove these break lines.

5
00:00:18,820 --> 00:00:20,680
But how do we go about this?

6
00:00:20,770 --> 00:00:22,870
Again, introduce the text.

7
00:00:22,900 --> 00:00:24,760
We don't have these break lines.

8
00:00:24,760 --> 00:00:28,210
Actually, we have break lines, but only one break line.

9
00:00:28,210 --> 00:00:35,260
So there is a backslash n after prepare and then there is another backslash n after shower and so on.

10
00:00:36,130 --> 00:00:42,130
So the extra break line is being produced by the print function here.

11
00:00:42,640 --> 00:00:47,230
So we end up with two sets of break lines for each line.

12
00:00:48,530 --> 00:00:51,290
So how do we go about this?

13
00:00:51,320 --> 00:00:56,720
Do we remove the brick lines from the text file or we do something in here?

14
00:00:56,960 --> 00:00:57,860
Well.

15
00:00:59,050 --> 00:01:02,600
Of course, we shouldn't edit the text file.

16
00:01:02,620 --> 00:01:07,900
We should edit the representation of the data that is in the text file.

17
00:01:07,900 --> 00:01:09,940
So the representation is the list.

18
00:01:09,940 --> 00:01:12,280
Therefore we should add it to the list.

19
00:01:12,280 --> 00:01:14,110
So that's the case.

20
00:01:14,680 --> 00:01:17,500
BLOCK And here we get.

21
00:01:18,670 --> 00:01:23,640
The to do list from the text file and then we iterate over that to do list.

22
00:01:23,650 --> 00:01:30,760
But now a solution we could think about is to remove.

23
00:01:35,700 --> 00:01:39,390
The backslash and characters from the list.

24
00:01:40,440 --> 00:01:50,730
Because currently, if we print out the list here, we execute the program again, we press show, and

25
00:01:50,730 --> 00:01:55,920
this time you're going to see that that's the print function that I've just added.

26
00:01:56,520 --> 00:01:59,970
So that print function produces that list.

27
00:01:59,970 --> 00:02:03,300
So the list has these backslash characters.

28
00:02:05,630 --> 00:02:07,210
And then the loop add some more.

29
00:02:07,220 --> 00:02:12,890
So let's remove this back slashes and then we fix the problem.

30
00:02:12,890 --> 00:02:16,100
So we remove the backslash from each item.

31
00:02:17,610 --> 00:02:21,450
Therefore, the loop will only produce one backslash.

32
00:02:22,730 --> 00:02:27,650
The backslash that it produces by default and it will not have any other backlashes.

33
00:02:28,430 --> 00:02:30,770
Therefore, how do we add it?

34
00:02:31,720 --> 00:02:33,610
Each of these items.

35
00:02:33,610 --> 00:02:37,510
So how do we perform batch operations?

36
00:02:37,510 --> 00:02:44,230
You learned four loops earlier in the course, so that is something you can use.

37
00:02:45,040 --> 00:02:46,960
There are two solutions for this actually.

38
00:02:46,960 --> 00:02:49,580
One is for loops and one is list compressions.

39
00:02:49,630 --> 00:02:51,430
And we're going to go through both.

40
00:02:51,430 --> 00:02:53,950
So let's first start with for loops.

41
00:02:54,760 --> 00:02:56,350
That's the existing list.

42
00:02:56,560 --> 00:03:00,040
But what I'll do is I'll create a new list here.

43
00:03:00,040 --> 00:03:09,970
So in the same indentation level, let's say new to dos variable is equal to an empty list.

44
00:03:10,660 --> 00:03:12,370
And then what we do is.

45
00:03:15,460 --> 00:03:20,250
We iterates over four item in to dos.

46
00:03:20,260 --> 00:03:30,130
We iterate over to dos so our item will be each item of the to do list, including the backslash n.

47
00:03:30,130 --> 00:03:31,300
And then.

48
00:03:32,880 --> 00:03:34,230
We say new.

49
00:03:35,870 --> 00:03:40,880
It will be equal to item that strip.

50
00:03:43,820 --> 00:03:45,020
Backslash.

51
00:03:46,940 --> 00:03:50,010
And so that's command.

52
00:03:50,030 --> 00:03:52,460
What it does is, is this.

53
00:03:53,330 --> 00:03:56,240
So let's say item will be equal to.

54
00:03:58,740 --> 00:04:00,000
Prepare.

55
00:04:02,080 --> 00:04:03,400
Backslash n.

56
00:04:05,250 --> 00:04:11,430
Therefore new item is equal to item strip.

57
00:04:14,150 --> 00:04:15,110
Backslash.

58
00:04:15,260 --> 00:04:26,000
NW So a new item now is going to be equal to just prepare without the backslash N while item is that

59
00:04:26,000 --> 00:04:29,810
one so new item will be without the backslash.

60
00:04:29,810 --> 00:04:40,160
And then what we do is we access new to DOS that append and we add new item there.

61
00:04:41,180 --> 00:04:47,690
So we're going to end up with a new list which is going to contain the items without the backslash and

62
00:04:48,020 --> 00:04:48,920
characters.

63
00:04:52,940 --> 00:04:58,820
Then what we could do is in here we iterate over new to dos instead of.

64
00:05:01,310 --> 00:05:02,180
To reduce.

65
00:05:03,280 --> 00:05:05,420
And that's with solve the issue.

66
00:05:05,440 --> 00:05:08,140
So now let me rerun the script.

67
00:05:10,630 --> 00:05:12,220
And type show.

68
00:05:13,400 --> 00:05:15,320
And these are the two do's.

69
00:05:16,730 --> 00:05:21,950
We also get the lists here from that print so we can remove that print function.

70
00:05:21,950 --> 00:05:23,410
We don't need it anymore.

71
00:05:23,420 --> 00:05:25,640
So that is one way to do it.

72
00:05:26,480 --> 00:05:31,700
Another way to clean up the list is to use a list comprehension.

73
00:05:32,000 --> 00:05:35,840
A list comprehension is the equivalent of a full loop.

74
00:05:35,840 --> 00:05:37,910
So it's just another way.

75
00:05:40,100 --> 00:05:44,990
To modify the items of the list, but it's a shorter way.

76
00:05:44,990 --> 00:05:49,040
You'll see why and list comprehension.

77
00:05:49,040 --> 00:05:56,210
All it takes is one single line, and it starts always with square brackets.

78
00:05:56,210 --> 00:06:08,030
So inside here you write an inline four loop, so a four loop in one line you would say item that strip.

79
00:06:10,520 --> 00:06:12,350
Backslash n.

80
00:06:13,560 --> 00:06:15,510
So that is one expression.

81
00:06:18,140 --> 00:06:20,810
And you say for item in.

82
00:06:22,290 --> 00:06:24,000
To dos.

83
00:06:25,320 --> 00:06:29,160
And this is going to be new to dos.

84
00:06:29,370 --> 00:06:31,500
So this is basically a list.

85
00:06:33,450 --> 00:06:35,700
I am going to remove these.

86
00:06:37,360 --> 00:06:38,830
We don't need them anymore.

87
00:06:38,830 --> 00:06:43,840
But before I remove them, just try to compare the full loop with this.

88
00:06:44,770 --> 00:06:51,600
So basically what we're saying is that you can start reading this from here to understand it.

89
00:06:51,610 --> 00:06:54,760
So for Autumn, introduce.

90
00:06:56,310 --> 00:07:02,850
You iterates over the to do list store item that strip backslash and in this list.

91
00:07:02,850 --> 00:07:10,440
So this will return an item without the backslash and so this item will be part of the list and this

92
00:07:10,440 --> 00:07:12,360
is repeated over the other items.

93
00:07:12,360 --> 00:07:15,330
So we end up with three items.

94
00:07:16,900 --> 00:07:19,090
As part of this list.

95
00:07:19,090 --> 00:07:21,370
So this will be just a list.

96
00:07:21,370 --> 00:07:23,020
So let me delete that now.

97
00:07:24,100 --> 00:07:26,860
And the declaration of the new to do list.

98
00:07:26,860 --> 00:07:31,900
So with the list comprehension, you don't even have to declare an empty list.

99
00:07:33,280 --> 00:07:37,090
You create the list on the fly using the list comprehension.

100
00:07:38,530 --> 00:07:39,730
So run.

101
00:07:42,730 --> 00:07:43,600
Show.

102
00:07:44,260 --> 00:07:49,540
And we get the same exact output using a list comprehension.

103
00:07:52,510 --> 00:07:59,950
However, perhaps we didn't even need to use a list comprehension here.

104
00:08:00,160 --> 00:08:08,380
We could apply a more direct method to remove the backslash and characters from.

105
00:08:09,430 --> 00:08:11,350
These to do items.

106
00:08:11,350 --> 00:08:17,080
So that way would be to delete this.

107
00:08:17,980 --> 00:08:22,840
The way is to iterate over to DOS as it was.

108
00:08:22,840 --> 00:08:29,020
So this was the code in the beginning of the video which produces again.

109
00:08:33,140 --> 00:08:36,500
These items with extra brake lines.

110
00:08:36,650 --> 00:08:38,539
And then what you could do is.

111
00:08:40,890 --> 00:08:44,130
You want to override the item variable?

112
00:08:44,159 --> 00:08:48,240
You could do something like item is equal to item strip.

113
00:08:49,680 --> 00:08:53,790
And then here you place backslash n.

114
00:08:55,440 --> 00:09:03,720
And then this item here will be the new string without the backslash n character.

115
00:09:04,470 --> 00:09:06,900
So let me try this solution as well.

116
00:09:11,260 --> 00:09:12,430
And here we go.

117
00:09:13,180 --> 00:09:16,840
So I suppose this was a simple solution, but.

118
00:09:18,120 --> 00:09:21,270
This was a good opportunity for you to understand.

119
00:09:23,340 --> 00:09:24,670
List comprehension.

120
00:09:24,690 --> 00:09:27,370
So whenever you want to modify.

121
00:09:28,900 --> 00:09:30,730
The items of the list.

122
00:09:30,730 --> 00:09:33,880
You want to use a list comprehension.

123
00:09:35,590 --> 00:09:43,270
And of course, luckily or unluckily, sometimes there are more than one solution to do something.

124
00:09:44,480 --> 00:09:46,640
Which one is the best?

125
00:09:46,670 --> 00:09:48,330
Well, it depends.

126
00:09:48,350 --> 00:09:51,770
You should think about readability, but also.

127
00:09:53,290 --> 00:09:54,160
Efficiency.

128
00:09:54,160 --> 00:09:57,790
So how code, how fast your code runs.

129
00:09:58,300 --> 00:10:02,230
And there are ways to measure the speed of your code.

130
00:10:03,230 --> 00:10:04,880
And we'll cover that later.

131
00:10:07,040 --> 00:10:12,200
For now, it is important to learn all the core elements of the language.

132
00:10:12,200 --> 00:10:14,230
So thank you for following this.

133
00:10:14,240 --> 00:10:15,690
I'll talk to you in the next videos.

