1
00:00:04,660 --> 00:00:09,320
Alright so in the last video, we talked about the two challenges but I didn't really explain in detail what

2
00:00:09,320 --> 00:00:10,040
they were.

3
00:00:10,280 --> 00:00:12,450
So the first file challenge1.py,

4
00:00:12,500 --> 00:00:15,560
remember you can get that from the resources section of the last video.

5
00:00:15,590 --> 00:00:21,200
The challenge for that first one is to rewrite the following code to use a list comprehension, instead

6
00:00:21,200 --> 00:00:25,050
of a for loop, and you can see the for loop on lines 10 and 11.

7
00:00:25,060 --> 00:00:30,830
Now you need to add your solution below the loop, so that the resulting list is printed out below the

8
00:00:30,850 --> 00:00:36,890
output, so it makes it easier to check that it's producing exactly the same list and avoids entering the input

9
00:00:36,890 --> 00:00:37,900
text twice.

10
00:00:38,150 --> 00:00:39,510
So basically, we're going to do that.

11
00:00:39,630 --> 00:00:41,830
So let's go ahead and look at that first.

12
00:00:41,870 --> 00:00:44,540
And you can see that we've helpfully got a comment here

13
00:00:44,570 --> 00:00:48,330
to say type your solution here, and that's exactly where we're going to start typing it.

14
00:00:48,710 --> 00:00:53,030
And by the way, I mentioned down here that it could be useful to store the original words in a list

15
00:00:53,030 --> 00:00:53,980
as well.

16
00:00:53,990 --> 00:00:55,900
The for loop would look like this;

17
00:00:56,090 --> 00:00:57,720
down here you can see the example of that,

18
00:00:57,890 --> 00:01:03,320
and notice the extra parentheses so that we get tuples in the list. You can see the extra parentheses there

19
00:01:03,320 --> 00:01:05,930
on line 23. But we'll get to that shortly.

20
00:01:05,930 --> 00:01:07,730
We're going to do this first bit first.

21
00:01:07,800 --> 00:01:13,850
So for the first part, our comprehension will iterate over the list that's produced by splitting up the

22
00:01:13,850 --> 00:01:14,990
input text.

23
00:01:14,990 --> 00:01:16,730
Now we want the length of each word,

24
00:01:17,030 --> 00:01:22,940
so the comprehension's going to use the len function for that. So lets type in the code and we can see how

25
00:01:22,940 --> 00:01:24,430
we go about that.

26
00:01:24,580 --> 00:01:33,540
So I'm going to start on line 16, answer is equal to, left and right square brackets because it's a comprehension,

27
00:01:33,640 --> 00:01:43,580
a list comprehension, len parentheses x for x in text dot split parentheses.

28
00:01:43,820 --> 00:01:50,290
Then on the next line we can print parentheses answers, or answer I should say.

29
00:01:51,080 --> 00:01:56,510
So basically, we want the length of each word and the comprehension's using the len x function, as you

30
00:01:56,510 --> 00:02:00,530
can see on line 16, the same as the for loop did. Nothing unique there.

31
00:02:00,830 --> 00:02:03,750
So lets run the program to make sure it works.

32
00:02:03,860 --> 00:02:06,220
And obviously, we'll see the for loop version running as well.

33
00:02:07,590 --> 00:02:12,210
Now for the text, we're going to go with What have the Romans

34
00:02:14,860 --> 00:02:16,390
ever done for us.

35
00:02:18,610 --> 00:02:24,430
So you can see that we've got the same output for both the for loop and also for the comprehension.

36
00:02:24,520 --> 00:02:25,730
So that's a good start.

37
00:02:25,850 --> 00:02:31,250
Now for this second part of the challenge, I mentioned that it could be useful to store the original words

38
00:02:31,250 --> 00:02:32,840
in the list as well.

39
00:02:32,840 --> 00:02:38,900
Now the for loop would look like the example you can see there with the extra parentheses, so that

40
00:02:38,900 --> 00:02:40,490
we get tuples in the list.

41
00:02:40,770 --> 00:02:46,250
And obviously, you're going to type your corresponding comprehension down here on, starting on line 29.

42
00:02:46,250 --> 00:02:51,570
So basically, we want a tuple containing the original word and it's length.

43
00:02:51,580 --> 00:02:54,200
Let's go ahead and type in an answer to that.

44
00:02:54,200 --> 00:03:04,550
And we'll start this one on line 30: answer is equal to left and right square brackets, parentheses x comma space len

45
00:03:05,240 --> 00:03:14,990
left and right parentheses, x then we've got two closing parentheses as you can say there, then 4 x in text dot split

46
00:03:15,230 --> 00:03:18,420
parentheses. Then we'll print the answer on the next line,

47
00:03:18,440 --> 00:03:25,670
print parentheses answer. So basically, is we're producing a tuple containing the word and its length.

48
00:03:25,720 --> 00:03:32,980
And what I'll do is, I'm just going to copy the exact same output that we typed in up here. Run the code,

49
00:03:43,470 --> 00:03:50,130
and once again, you can see our comprehension produces the same list as the original for loop did. Converting

50
00:03:50,130 --> 00:03:55,650
a for loop to a comprehension is quite straightforward, and they're a very useful addition to your programming

51
00:03:55,650 --> 00:03:56,490
tool kit.

52
00:03:56,590 --> 00:04:00,930
Now before I move onto the solution to the second challenge, there's a couple of things to say about

53
00:04:00,930 --> 00:04:01,960
this one.

54
00:04:02,010 --> 00:04:08,040
Now the first is, we probably don't want duplicates. In other words, if the same word appears more than once

55
00:04:08,040 --> 00:04:09,040
in the input,

56
00:04:09,090 --> 00:04:11,260
there's no point in counting it twice.

57
00:04:11,640 --> 00:04:13,080
So to get around that,

58
00:04:13,140 --> 00:04:16,740
what we could do is use a set comprehension instead.

59
00:04:17,000 --> 00:04:21,360
Now we've already seen how to do that, to convert a list comprehension to a set comprehension.

60
00:04:21,420 --> 00:04:24,580
We just replace the square brackets with curly braces.

61
00:04:24,590 --> 00:04:25,710
So let's go ahead and do that,

62
00:04:30,060 --> 00:04:34,500
and let's run that. Now I'm going to provide a different input string here,

63
00:04:34,530 --> 00:04:36,430
so we've got some duplicated words.

64
00:04:36,720 --> 00:04:42,670
So the input text contains repeated words in the input.

65
00:04:47,710 --> 00:04:54,070
OK, save that, and you can see in this example down here now, we get a set returned,

66
00:04:54,270 --> 00:04:58,760
and because sets don't contain duplicates, we get each word only once.

67
00:04:58,770 --> 00:05:01,980
Now note that the words aren't necessarily appearing in order.

68
00:05:01,980 --> 00:05:08,620
In fact, they're definitely not in order as you can see here. We've got the word 'contains' here, c is after the letter r

69
00:05:09,120 --> 00:05:13,500
alphabetically, and that's because by definition there's no ordering in a set.

70
00:05:13,500 --> 00:05:18,410
Now you'll probably be doing this to produce some sort of metrics, such as an average word length for example,

71
00:05:18,780 --> 00:05:22,080
and therefore the ordering wouldn't be important.

72
00:05:22,080 --> 00:05:24,690
Now when you're considering using a comprehension,

73
00:05:24,750 --> 00:05:28,650
remember that set comprehensions can also be quite useful.

74
00:05:28,710 --> 00:05:33,810
They work the same as list comprehensions and can be very useful for avoiding duplicates.

75
00:05:34,120 --> 00:05:37,140
OK so the next thing isn't really a problem with comprehensions.

76
00:05:37,380 --> 00:05:39,750
It's got to do with the way that split works.

77
00:05:40,110 --> 00:05:42,120
So we split our text up to get words,

78
00:05:42,270 --> 00:05:48,570
and it works fine as long as each word's separated from the next with white space - white spaces, space tabs

79
00:05:48,570 --> 00:05:53,970
and the new line characters, although we can't enter line breaks in the input. Now to see what I mean,

80
00:05:54,210 --> 00:05:58,000
let's run the program again and use some punctuation.

81
00:05:58,380 --> 00:06:02,010
So this time we're going to enter What have they

82
00:06:02,190 --> 00:06:10,360
comma the Romans comma ever done for us, ever done for us

83
00:06:10,560 --> 00:06:18,200
with a question mark on the end, enter. Now the program still works, but they now show, for example, the word 'they' over

84
00:06:18,210 --> 00:06:24,000
here, as being of length five, five characters, and that's because of the comma after the word 'they'.

85
00:06:24,160 --> 00:06:30,210
We've also got 'Romans' showing a seven as well, and 'us' even having three over here, and that's because of

86
00:06:30,210 --> 00:06:31,090
the question mark.

87
00:06:31,260 --> 00:06:35,450
Now I'm mentioning this because we can use a nested comprehension to fix it.

88
00:06:35,460 --> 00:06:40,520
Remember the problem, and we'll come back to it when we look at nesting comprehensions later.

89
00:06:40,550 --> 00:06:40,780
Right,

90
00:06:40,780 --> 00:06:44,410
so that's the first challenge. I'm going to close down that challenge1.py now.

91
00:06:45,230 --> 00:06:51,130
So the second challenge, we had to create a list comprehension to convert a tuple from inches to centimetres.

92
00:06:51,740 --> 00:06:57,470
And just as a note here, in case it's not obvious, a list comprehension produces a list, but it doesn't

93
00:06:57,470 --> 00:06:59,540
have to be given a list to iterate over.

94
00:06:59,840 --> 00:07:02,640
Now you can use a list comprehension with any iterable type.

95
00:07:02,720 --> 00:07:06,500
So we're going to write a comprehension here - that was your challenge - to convert dimensions from

96
00:07:06,500 --> 00:07:08,600
inches to centimetres.

97
00:07:08,680 --> 00:07:14,700
Now our dimensions will be replaced by tuple for the length, width and height, and as a reminder it's 2.5

98
00:07:14,770 --> 00:07:16,560
4 centimeters to 1 inch.

99
00:07:16,940 --> 00:07:21,800
And then down here, once you've got the correct values, change the code to produce a tuple rather

100
00:07:21,800 --> 00:07:22,910
than a list.

101
00:07:23,140 --> 00:07:27,950
Alright, so we've got our code there printing out the measurement, and again in case it's not obvious, a list

102
00:07:27,950 --> 00:07:32,960
comprehension produces a list, but it doesn't have to be given the list to iterate over - that's the important

103
00:07:32,960 --> 00:07:33,590
part.

104
00:07:33,680 --> 00:07:37,970
And that's really why I actually created this challenge in the first place, really just

105
00:07:37,970 --> 00:07:41,950
to demonstrate that a comprehension can use any iterable type or sequence.

106
00:07:42,050 --> 00:07:47,840
You don't have to provide a list to a list comprehension. So the list, or set, in list comprehension refers

107
00:07:47,840 --> 00:07:54,790
to the type of object produced by the comprehension. Alright, so we've got the value here, so on line 13, let's now

108
00:07:54,890 --> 00:08:01,010
enter our comprehension. I'm going to delete that code there in between the square brackets,

109
00:08:01,010 --> 00:08:02,320
and we're going to go with inch

110
00:08:04,970 --> 00:08:13,670
multiplied by two point five four for inch in inch underscore measurement.

111
00:08:13,670 --> 00:08:16,830
I'll leave the code to print the cm underscore measurement.

112
00:08:17,240 --> 00:08:23,960
So the comprehension iterates over each item in the tuple, and multiplies it by the number of centimeters

113
00:08:24,010 --> 00:08:26,190
in an inch, two point five four.

114
00:08:26,480 --> 00:08:31,420
So let's actually run this to confirm that it works.

115
00:08:31,800 --> 00:08:34,059
And you can see we've correctly got the values there,

116
00:08:34,110 --> 00:08:39,179
seven point six two, twenty point three two and fifty point eight. Alright, now our

117
00:08:39,179 --> 00:08:43,659
next part here, we need to convert the code to produce a tuple rather than a list.

118
00:08:43,919 --> 00:08:48,890
All we have to do is parse the comprehension to the tuple function to get that to work.

119
00:08:48,920 --> 00:08:55,860
So let's add the code there to do that. Now line 18, we'll do a cm underscore measurement is equal to tuple,

120
00:08:57,450 --> 00:09:03,660
then we want parentheses, then we need to add the list comprehension to the tuple call. So in left to right

121
00:09:03,690 --> 00:09:04,860
square brackets,

122
00:09:04,910 --> 00:09:05,960
it's going to be -

123
00:09:05,980 --> 00:09:12,690
now I could just paste that code in, but I'll just quickly type it in - inch times two point five four, for

124
00:09:12,690 --> 00:09:20,320
inch in inch underscore measurement. And then on the last line we'll print out

125
00:09:21,000 --> 00:09:23,070
cm_measurement again.

126
00:09:23,070 --> 00:09:30,020
So basically, this call here on line 18 is converting a tuple of values in one unit, inches here, to an

127
00:09:30,020 --> 00:09:33,990
equivalent tuple using another set of units, centimetres.

128
00:09:34,040 --> 00:09:36,420
And I think that's pretty impressive for one line of code,

129
00:09:36,610 --> 00:09:39,440
but lets actually just run it to make sure we do get the right results.

130
00:09:41,600 --> 00:09:45,130
We've now got the identical results in a tuple.

131
00:09:45,520 --> 00:09:51,540
Alright, so I'm going to stop the video here. In the next one, we're going to look at adding conditions to our comprehensions.

132
00:09:51,590 --> 00:09:52,980
So I'll see you in the next video.

