1
00:00:01,290 --> 00:00:02,730
Instructor: All right, the first thing to notice

2
00:00:02,730 --> 00:00:06,780
about this exercise is that it is hard.

3
00:00:06,780 --> 00:00:08,340
So I just want to let you know

4
00:00:08,340 --> 00:00:10,110
that we're doing list comprehension

5
00:00:10,110 --> 00:00:13,200
but the previous two were a little bit on the easier side.

6
00:00:13,200 --> 00:00:14,940
And now that we've got our feet wet,

7
00:00:14,940 --> 00:00:17,880
I think we're ready for a real challenge.

8
00:00:17,880 --> 00:00:20,430
So just set your expectations.

9
00:00:20,430 --> 00:00:23,940
It's not gonna be something that you can breeze through.

10
00:00:23,940 --> 00:00:25,320
Now, I want you to take a look

11
00:00:25,320 --> 00:00:30,320
through the description and see what the goal is.

12
00:00:30,360 --> 00:00:35,360
We've got two external files, file1.txt and file2.txt.

13
00:00:36,510 --> 00:00:41,510
And your challenge is to create a list called result

14
00:00:42,000 --> 00:00:46,380
that contains the numbers that are common to both files.

15
00:00:46,380 --> 00:00:49,950
So this happens surprisingly often.

16
00:00:49,950 --> 00:00:53,430
I found myself using Python for this to check, for example,

17
00:00:53,430 --> 00:00:57,030
if my list of favorite movies is in the same list

18
00:00:57,030 --> 00:01:00,330
as my friend's list of favorite movies, or if my pick

19
00:01:00,330 --> 00:01:04,680
of fantasy football players is the same as my friend's.

20
00:01:04,680 --> 00:01:09,150
And this is going to be done using list comprehension.

21
00:01:09,150 --> 00:01:10,950
Now, if you look through the description,

22
00:01:10,950 --> 00:01:12,840
you'll see a number of hints

23
00:01:12,840 --> 00:01:15,960
and they are there to help you.

24
00:01:15,960 --> 00:01:18,960
As you get stuck, you should try to take a look

25
00:01:18,960 --> 00:01:21,060
at the hint and see incrementally

26
00:01:21,060 --> 00:01:24,780
if each of them can help you through the challenges.

27
00:01:24,780 --> 00:01:28,380
But just be aware, it is going to be a struggle.

28
00:01:28,380 --> 00:01:31,980
Now, what you're expecting is in the example output,

29
00:01:31,980 --> 00:01:34,620
this is the goal that we're trying to achieve.

30
00:01:34,620 --> 00:01:36,930
So the first step to prepare is read

31
00:01:36,930 --> 00:01:39,060
through the instructions.

32
00:01:39,060 --> 00:01:41,610
The next step is I want you to take a look

33
00:01:41,610 --> 00:01:45,270
at file1.txt and file2.txt.

34
00:01:45,270 --> 00:01:47,310
Notice, these are not editable.

35
00:01:47,310 --> 00:01:48,960
We've made them read only

36
00:01:48,960 --> 00:01:52,170
so that you don't accidentally change a number

37
00:01:52,170 --> 00:01:53,430
and something goes wrong

38
00:01:53,430 --> 00:01:56,250
or the result doesn't match, et cetera.

39
00:01:56,250 --> 00:01:58,260
File1.txt text contains some numbers,

40
00:01:58,260 --> 00:02:02,610
one on each line, and file2.txt contains more numbers,

41
00:02:02,610 --> 00:02:04,320
not the same number of numbers,

42
00:02:04,320 --> 00:02:07,050
and it's also one on each line.

43
00:02:07,050 --> 00:02:11,460
Your job is to import those numbers from those two files,

44
00:02:11,460 --> 00:02:14,790
use this comprehension to compare those two files

45
00:02:14,790 --> 00:02:18,570
and output something called result that contains the numbers

46
00:02:18,570 --> 00:02:20,640
which are common to both files.

47
00:02:20,640 --> 00:02:24,330
That is the goal, and I'll leave you to figure it out.

48
00:02:24,330 --> 00:02:25,263
So good luck.

49
00:02:30,000 --> 00:02:35,000
Now, first step is to use Python to open up our files.

50
00:02:35,070 --> 00:02:39,060
You can see on line one, we're using the with keyword

51
00:02:39,060 --> 00:02:42,903
to open file1.txt as file1.

52
00:02:43,740 --> 00:02:48,330
And then we are using file1.readlines

53
00:02:48,330 --> 00:02:53,283
in order to create a list from all the lines in that file.

54
00:02:55,050 --> 00:03:00,000
Step two is to perform the same action on file2

55
00:03:00,000 --> 00:03:03,420
and get a list that contains a number

56
00:03:03,420 --> 00:03:05,790
on each line of that file.

57
00:03:05,790 --> 00:03:08,940
Readlines is a method that we needed

58
00:03:08,940 --> 00:03:11,730
in order to complete this action, and I left it

59
00:03:11,730 --> 00:03:14,520
in the hints, but hopefully at this point, you're familiar

60
00:03:14,520 --> 00:03:17,190
with googling for what you need on the internet as well

61
00:03:17,190 --> 00:03:19,410
and you might have found it that way.

62
00:03:19,410 --> 00:03:21,510
Now, the next step is the hard step.

63
00:03:21,510 --> 00:03:24,600
This is our actual list comprehension.

64
00:03:24,600 --> 00:03:26,730
Let's start off with the for loop.

65
00:03:26,730 --> 00:03:30,550
So on line seven, you can see we're looping through list1

66
00:03:31,620 --> 00:03:34,410
and we are getting hold of each number

67
00:03:34,410 --> 00:03:36,630
using the variable num.

68
00:03:36,630 --> 00:03:41,630
So for num in list1, if num is in list2,

69
00:03:42,980 --> 00:03:46,680
so the in keyword here, you see in show up twice

70
00:03:46,680 --> 00:03:48,870
in this line, but they actually mean different things

71
00:03:48,870 --> 00:03:50,550
and they do different things.

72
00:03:50,550 --> 00:03:53,910
The first one is a for in loop, which means

73
00:03:53,910 --> 00:03:57,990
in a particular list, loop through each one of the items.

74
00:03:57,990 --> 00:04:02,910
But if numb in list2 is actually a condition.

75
00:04:02,910 --> 00:04:04,950
It's a check to see

76
00:04:04,950 --> 00:04:08,880
if the number that we're currently looping in through list1

77
00:04:08,880 --> 00:04:11,100
exists in list2.

78
00:04:11,100 --> 00:04:14,070
And this is a very short and succinct way

79
00:04:14,070 --> 00:04:15,990
of achieving our goals,

80
00:04:15,990 --> 00:04:19,290
but it is not the most straightforward to read

81
00:04:19,290 --> 00:04:20,123
and to understand.

82
00:04:20,123 --> 00:04:23,670
You might have to think about it a few times.

83
00:04:23,670 --> 00:04:25,260
You can try expanding it out

84
00:04:25,260 --> 00:04:29,790
into a full for loop and a full if statement.

85
00:04:29,790 --> 00:04:32,100
But essentially, the first step is to loop

86
00:04:32,100 --> 00:04:33,660
through each number in list1.

87
00:04:33,660 --> 00:04:35,280
Second step is to check each

88
00:04:35,280 --> 00:04:38,310
of those numbers against every item in list2.

89
00:04:38,310 --> 00:04:40,830
And then we end up with the values that match

90
00:04:40,830 --> 00:04:45,830
that criteria and we add it into our result

91
00:04:46,170 --> 00:04:50,403
and we create a new list using those results.

92
00:04:52,320 --> 00:04:54,900
Now, the final thing to remember is that in order

93
00:04:54,900 --> 00:04:59,760
for the result that's printed to match the tests,

94
00:04:59,760 --> 00:05:03,360
the result has to be a list of integers.

95
00:05:03,360 --> 00:05:07,170
So the final step before adding our num

96
00:05:07,170 --> 00:05:10,980
into the list is to convert it into an integer.

97
00:05:10,980 --> 00:05:15,300
And we do that at the first part of our list comprehension.

98
00:05:15,300 --> 00:05:17,940
And now with these few lines of code,

99
00:05:17,940 --> 00:05:20,970
we would've achieved the goals of the challenge

100
00:05:20,970 --> 00:05:25,970
and we would end up with a result that passes all the tests.

101
00:05:26,280 --> 00:05:27,113
So this is the point,

102
00:05:27,113 --> 00:05:29,460
if you need to go back to fix your solution,

103
00:05:29,460 --> 00:05:31,470
then it's time to go and do that.

104
00:05:31,470 --> 00:05:33,030
If you manage to achieve everything,

105
00:05:33,030 --> 00:05:35,760
then congratulations, a huge pat on the back.

106
00:05:35,760 --> 00:05:38,400
This is not easy. I know.

107
00:05:38,400 --> 00:05:40,080
And once you're happy with the code,

108
00:05:40,080 --> 00:05:42,453
then proceed on to the next lesson.

