1
00:00:05,630 --> 00:00:08,300
Alright so moving now onto Challenge 2.

2
00:00:08,570 --> 00:00:14,460
And I've gone through the details of what this challenge was, in, not the previous video, the one before that.

3
00:00:14,690 --> 00:00:17,760
And we've also got an outline here but I won't go through it again.

4
00:00:17,780 --> 00:00:21,110
What I will do though, we'll start talking about the expression we want to use.

5
00:00:21,350 --> 00:00:26,810
So basically, we want to provide the items for the list, so the expression we want to use is the value

6
00:00:26,810 --> 00:00:28,680
from the locations dictionary,

7
00:00:28,880 --> 00:00:32,880
that's locations exit in square brackets.

8
00:00:32,900 --> 00:00:37,910
Now we do that for each of the keys in the exits dictionary.

9
00:00:38,000 --> 00:00:41,550
We're going to filter out, or filtering out, any values that don't contain the forests

10
00:00:41,560 --> 00:00:43,970
key, 5 in this case.

11
00:00:43,970 --> 00:00:46,660
So let's start running some code to understand what we're doing here.

12
00:00:46,980 --> 00:00:51,080
So I'm going to start by adding some code on line 35,

13
00:00:51,110 --> 00:01:00,350
actually we'll start on line 36, loc location equals 5. I'm just going to move up to make it a bit clearer in case you've

14
00:01:00,350 --> 00:01:10,990
got subtitles on. So forest is equal to left square bracket locations exit in square brackets, for exit

15
00:01:11,810 --> 00:01:15,130
in exits, and then we're going to add an if here, so

16
00:01:15,230 --> 00:01:27,210
if loc, l o c,  in exits left square brackets exit right square bracket dot values parentheses, and then we've got

17
00:01:27,310 --> 00:01:29,880
our right square bracket. Then we're

18
00:01:29,970 --> 00:01:32,890
going to print the forest.

19
00:01:32,900 --> 00:01:38,400
So again, the expression we're using here, to provide the items for the list, is the value from the locations

20
00:01:38,540 --> 00:01:39,560
dictionary,

21
00:01:39,560 --> 00:01:41,870
and that's locations exit.

22
00:01:41,900 --> 00:01:47,510
Now we do that for each of the keys in the exits dictionary, filtering out any values that don't contain

23
00:01:47,510 --> 00:01:51,090
the forest key, which you can see here we've assigned on line 36,

24
00:01:51,200 --> 00:01:57,320
location 5, and I assigned that, as you can see, to the variable loc just to make it easier to test different locations.

25
00:01:57,320 --> 00:02:04,660
So if we run this program now, you can see we get the expected results: the road, as well as the hill.

26
00:02:05,500 --> 00:02:07,570
Alright, now to get the list for the road,

27
00:02:07,570 --> 00:02:11,380
it's a simple case of just changing the loc to one, the location to one.

28
00:02:11,420 --> 00:02:13,470
And I'll move this down a little bit.

29
00:02:13,700 --> 00:02:21,160
Lets change the location to one, and run it again, and you can see this time we're getting the building, the

30
00:02:21,160 --> 00:02:24,260
valley and the forest as output.

31
00:02:24,470 --> 00:02:28,170
Alright, so the next part of the challenge was to produce a list of tuples.

32
00:02:28,450 --> 00:02:33,820
So let's copy the comprehension we've already got, and then change the expression to include the value

33
00:02:33,820 --> 00:02:34,750
of exit.

34
00:02:34,780 --> 00:02:35,950
So I'm going to copy these two lines,

35
00:02:39,420 --> 00:02:42,460
and we're modifying the expression part.

36
00:02:42,460 --> 00:02:47,030
So we're going to come down here, so where this is locations, you want to change that and put parentheses

37
00:02:47,030 --> 00:02:48,450
around that.

38
00:02:49,140 --> 00:02:57,450
That's going to be exit comma space locations square bracket exit then right parentheses, then for exit

39
00:02:57,460 --> 00:03:04,820
in exits if loc in exits, square brackets exit, dot values, the parentheses and the right square bracket,

40
00:03:05,230 --> 00:03:10,400
and then print it out. So then when we run that, when we run that, we should get the locations key as well as

41
00:03:10,400 --> 00:03:11,300
the description,

42
00:03:11,300 --> 00:03:15,420
remembering that exit is iterating over the keys in locations.

43
00:03:15,530 --> 00:03:19,330
So let's try running that,

44
00:03:19,600 --> 00:03:22,910
and you can see that we've also got the locations key,

45
00:03:22,990 --> 00:03:24,890
now returning for us as well.

46
00:03:24,910 --> 00:03:30,490
Now before we move onto the final part, you may have spotted something in this code, and it's to do with our

47
00:03:30,490 --> 00:03:33,380
control variable, exit in this case.

48
00:03:33,380 --> 00:03:39,280
We're getting an error if I highlight it, or warning rather; shadows or exit shadows builtin name exit.

49
00:03:39,490 --> 00:03:45,550
Now it's not really a problem here, because exit only exists in the context of the comprehension in Python

50
00:03:45,550 --> 00:03:46,730
3 at least,

51
00:03:46,810 --> 00:03:48,750
but I suggest renaming it anyway.

52
00:03:48,880 --> 00:03:55,290
So let's go ahead and do that, we're going to right-click that and do a refactor, rename.

53
00:03:55,600 --> 00:04:02,760
Let's just call it xit instead of exit, refactor. What I wanted to point out though, is that the second

54
00:04:02,760 --> 00:04:09,030
comprehension hasn't been changed. Though they've both used the same name, exit, it refers to different

55
00:04:09,030 --> 00:04:10,430
variables

56
00:04:10,500 --> 00:04:11,490
in each case.

57
00:04:11,590 --> 00:04:15,160
So the scope of exit is confined to the comprehension.

58
00:04:15,450 --> 00:04:20,769
So I mentioned that in an earlier video, and this hopefully now demonstrates that quite nicely.

59
00:04:21,320 --> 00:04:27,570
OK so moving on, now rather than changing the value of loc on line 36 to process each of the locations,

60
00:04:27,930 --> 00:04:33,390
what we can do instead, is put together a for loop around a second comprehension.

61
00:04:33,390 --> 00:04:43,530
So let's go ahead and do that, put for loc in sorted parentheses locations closing parentheses and a

62
00:04:43,530 --> 00:04:44,620
colon.

63
00:04:44,640 --> 00:04:45,800
What I'm going to do is indent

64
00:04:45,820 --> 00:04:50,530
that code, but we'll also add another bit of code here.

65
00:04:50,830 --> 00:04:58,070
Print parentheses and double quotes locations leading to,

66
00:04:58,350 --> 00:05:01,660
left and right curly brace, closing double quote dot format,

67
00:05:01,920 --> 00:05:05,250
parentheses loc, right parentheses

68
00:05:05,530 --> 00:05:13,510
comma, end equals single quote, backslash t for our tab character, and right parentheses.

69
00:05:13,620 --> 00:05:15,380
So that should make things look a bit better.

70
00:05:15,390 --> 00:05:18,490
Then we'll leave the next line in there, printing out forest.

71
00:05:18,860 --> 00:05:21,100
So let's try running that and see how it goes.

72
00:05:23,950 --> 00:05:29,140
So basically, as you've see there, I've added some text to the output so that each line shows the location that

73
00:05:29,140 --> 00:05:35,700
we're checking, and also sorted the keys before iterating over them just to make the output more readable.

74
00:05:35,740 --> 00:05:41,410
Now they're just frills. As long as your loop created a list for each location, then that's fine.

75
00:05:41,770 --> 00:05:45,670
And there's actually no need to sort the keys in Python 3.6.

76
00:05:45,670 --> 00:05:54,330
I'm talking about the code here, in the, the code on line 40, the for loc in sorted parentheses locations.

77
00:05:54,420 --> 00:06:00,270
So there's no need to do that, but the documentation does state this behavior shouldn't be relied on.

78
00:06:00,510 --> 00:06:05,630
So I've run the program as you can see to get the key in description, for every location that leads to

79
00:06:05,640 --> 00:06:07,150
each of the others.

80
00:06:07,200 --> 00:06:14,250
Now location 3, inside the building and location 4, the valley, well they can only be reached by the road,

81
00:06:14,460 --> 00:06:19,930
so you should only get the road for those two locations, and you can see that on our screen there, 3

82
00:06:19,930 --> 00:06:22,210
and 4 are showing just that.

83
00:06:22,590 --> 00:06:27,810
Now we should probably rename the list because forest was only a really good name when we were getting

84
00:06:27,810 --> 00:06:30,030
the links to location 5, the forest.

85
00:06:30,390 --> 00:06:36,630
And even then, using a specific location name, rather than a name that describes a list wasn't a good

86
00:06:36,630 --> 00:06:37,810
choice.

87
00:06:37,860 --> 00:06:43,730
So something like exits underscore to underscore destination perhaps, would be a more suitable name here.

88
00:06:44,070 --> 00:06:49,350
Now the challenge was included to show you that you can mix for loops and comprehensions.

89
00:06:49,350 --> 00:06:54,120
Now it is possible to produce the same set of lists with a nested comprehension,

90
00:06:54,150 --> 00:06:56,580
and we're going to be looking at those in the next video actually.

91
00:06:58,660 --> 00:07:01,720
Right so this is just a quick discussion about Challenge 3,

92
00:07:01,740 --> 00:07:03,480
just to remind you of what we were doing there.

93
00:07:03,690 --> 00:07:09,010
So for this challenge, the idea was to convert all comprehensions in the previous challenge to for

94
00:07:09,010 --> 00:07:09,600
loops.

95
00:07:09,810 --> 00:07:13,780
So we start off by creating a list comprehension from a for loop.

96
00:07:13,780 --> 00:07:19,170
This challenge is to go the other way: convert each of the comprehensions in the previous challenge into

97
00:07:19,170 --> 00:07:21,810
a for loop that produces the same result.

98
00:07:22,020 --> 00:07:27,400
Alright so let's have a look at the solution for that. Alright so if we have a look now, I'll just close down the run

99
00:07:27,400 --> 00:07:29,400
window now, to make a bit of space here.

100
00:07:29,780 --> 00:07:32,760
So we'll start putting some code in here, as of line 40.

101
00:07:33,100 --> 00:07:44,010
So we'll put forest equals an empty list left and right square brackets, for exit for xit in exits colon.

102
00:07:44,020 --> 00:07:53,010
Then we're going to put if loc in exits left square bracket xit right square bracket dot values parentheses colon,

103
00:07:54,230 --> 00:08:00,100
then forest dot append parentheses locations left square bracket,

104
00:08:00,310 --> 00:08:04,960
xit right square bracket, and then a closing parentheses, right parentheses, and then we're going

105
00:08:04,960 --> 00:08:07,740
to print forest.

106
00:08:10,960 --> 00:08:15,320
OK so lets try running that. Well actually we'll add another print there to separate the output as well.

107
00:08:16,110 --> 00:08:21,820
Let's try running that now, and you can see these first two lines of output.

108
00:08:22,130 --> 00:08:25,930
So we're obviously getting the same list printed twice. They should be identical and you can see in this case

109
00:08:25,930 --> 00:08:31,060
they are identical, because the for loop's now doing the same thing as the comprehension.

110
00:08:31,060 --> 00:08:36,830
Now here it may be important to call the control variable xit rather than exit, because it'll be

111
00:08:36,830 --> 00:08:38,570
the inner global namespace,

112
00:08:38,770 --> 00:08:44,350
so it'll prevent you from using the builtin exit function if you shadow the name exit here.

113
00:08:44,480 --> 00:08:48,310
Now of course, if your program isn't going to use the builtin exit function then this probably doesn't

114
00:08:48,310 --> 00:08:48,920
matter.

115
00:08:49,390 --> 00:08:53,000
But as a general rule, shadowing builtin names isn't a good idea though.

116
00:08:53,380 --> 00:08:58,120
So I suggest you do take note of the warnings that you get from your IDE, and try to avoid this sort of

117
00:08:58,120 --> 00:08:59,920
thing whenever possible.

118
00:08:59,920 --> 00:09:05,170
And I added an extra print statement as you saw down here on line 46, to print a blank line between the

119
00:09:05,170 --> 00:09:08,290
two solutions, just to make the output easier to read.

120
00:09:08,290 --> 00:09:11,890
Now the second comprehension, by the way, is very similar.

121
00:09:11,980 --> 00:09:18,180
In fact, we can copy that loop and paste it into the for loc loop replacing the comprehension.

122
00:09:18,550 --> 00:09:21,120
Alight so we've got our printout there so let's go ahead and copy this

123
00:09:21,120 --> 00:09:23,320
code. So we're going to start with,

124
00:09:23,320 --> 00:09:30,040
copy that forest line to initialize it in the loop, let's move this up a little bit so we can see, and we'll obviously

125
00:09:30,040 --> 00:09:32,070
replace the other version. Or what I'll do

126
00:09:32,120 --> 00:09:35,160
before I start, I'll just undo that,

127
00:09:35,500 --> 00:09:41,050
and we'll take a copy of that, paste it, and I'll just comment out the old version so you've got that code if you need to.

128
00:09:41,730 --> 00:09:46,840
And so with this new version we're going to again, change forest.

129
00:09:46,990 --> 00:09:52,330
We're going to just create an empty list initially, and then we're just going to copy that other code.

130
00:09:52,330 --> 00:09:58,600
So I'm going to copy this for loop with the if and the append. Lets copy all of that.

131
00:09:58,600 --> 00:10:00,290
Copy that down to below forest,

132
00:10:04,350 --> 00:10:06,980
and we make sure that we've got the printing out now in the right order.

133
00:10:07,000 --> 00:10:14,730
So we've got print and print forest, and then we need to change the appended value to be a tuple, which we haven't

134
00:10:14,730 --> 00:10:16,050
got at the moment in the append line.

135
00:10:16,110 --> 00:10:18,420
So this is the code on line 56.

136
00:10:18,420 --> 00:10:19,800
Alright so we now need to change

137
00:10:19,800 --> 00:10:25,680
the append line, so I'll just delete it and rewrite it which is probably easier. So that's two parentheses now because we're

138
00:10:25,680 --> 00:10:30,330
dealing with a tuple, xit comma space locations,

139
00:10:30,600 --> 00:10:37,060
left square bracket xit right square bracket closing right parentheses times two.

140
00:10:37,420 --> 00:10:38,760
And that should be that.

141
00:10:39,210 --> 00:10:41,040
Alright so that should be the code there, so lets try running that,

142
00:10:43,900 --> 00:10:47,030
and you can see for the first one we've got all of the above,

143
00:10:47,030 --> 00:10:48,470
all of the locations showing.

144
00:10:51,470 --> 00:10:52,310
And they all potentially lead to zero,

145
00:10:52,310 --> 00:10:53,070
number one,

146
00:10:53,190 --> 00:10:59,030
we've got three, four and five, number two, we've got one, four and five,

147
00:10:59,030 --> 00:11:01,280
number three we've got one,

148
00:11:01,280 --> 00:11:03,500
and number four we've got one. We talked about that before.

149
00:11:03,680 --> 00:11:09,390
And location 5 is only one and two, just the road and the hill.

150
00:11:09,510 --> 00:11:13,080
Alright so that's the two challenges now completed in this section.

151
00:11:13,080 --> 00:11:18,270
Now if you're still struggling to understand comprehensions, then try converting them back into a for

152
00:11:18,270 --> 00:11:22,510
loop. By doing that it really can help you understand what they're doing.

153
00:11:22,830 --> 00:11:28,020
And in the next video we going to look at nested comprehensions, which work just like the concept of nested

154
00:11:28,020 --> 00:11:29,290
for loops.

155
00:11:29,430 --> 00:11:34,980
Now nested comprehensions can be very useful, but they can also be quite hard to read.

156
00:11:34,980 --> 00:11:38,880
Now once we've seen what they are, we'll use one for this last solution.

157
00:11:38,880 --> 00:11:43,530
You'll then have three different ways to do the same thing, and you hopefully will get a good feel for when

158
00:11:43,530 --> 00:11:46,090
you might want to use each approach.

159
00:11:46,110 --> 00:11:48,030
So as always, see you in the next video.

