Instructor: All right, the first thing to notice
about this exercise is that it is hard.
So I just want to let you know
that we're doing list comprehension
but the previous two were a little bit on the easier side.
And now that we've got our feet wet,
I think we're ready for a real challenge.
So just set your expectations.
It's not gonna be something that you can breeze through.
Now, I want you to take a look
through the description and see what the goal is.
We've got two external files, file1.txt and file2.txt.
And your challenge is to create a list called result
that contains the numbers that are common to both files.
So this happens surprisingly often.
I found myself using Python for this to check, for example,
if my list of favorite movies is in the same list
as my friend's list of favorite movies, or if my pick
of fantasy football players is the same as my friend's.
And this is going to be done using list comprehension.
Now, if you look through the description,
you'll see a number of hints
and they are there to help you.
As you get stuck, you should try to take a look
at the hint and see incrementally
if each of them can help you through the challenges.
But just be aware, it is going to be a struggle.
Now, what you're expecting is in the example output,
this is the goal that we're trying to achieve.
So the first step to prepare is read
through the instructions.
The next step is I want you to take a look
at file1.txt and file2.txt.
Notice, these are not editable.
We've made them read only
so that you don't accidentally change a number
and something goes wrong
or the result doesn't match, et cetera.
File1.txt text contains some numbers,
one on each line, and file2.txt contains more numbers,
not the same number of numbers,
and it's also one on each line.
Your job is to import those numbers from those two files,
use this comprehension to compare those two files
and output something called result that contains the numbers
which are common to both files.
That is the goal, and I'll leave you to figure it out.
So good luck.
Now, first step is to use Python to open up our files.
You can see on line one, we're using the with keyword
to open file1.txt as file1.
And then we are using file1.readlines
in order to create a list from all the lines in that file.
Step two is to perform the same action on file2
and get a list that contains a number
on each line of that file.
Readlines is a method that we needed
in order to complete this action, and I left it
in the hints, but hopefully at this point, you're familiar
with googling for what you need on the internet as well
and you might have found it that way.
Now, the next step is the hard step.
This is our actual list comprehension.
Let's start off with the for loop.
So on line seven, you can see we're looping through list1
and we are getting hold of each number
using the variable num.
So for num in list1, if num is in list2,
so the in keyword here, you see in show up twice
in this line, but they actually mean different things
and they do different things.
The first one is a for in loop, which means
in a particular list, loop through each one of the items.
But if numb in list2 is actually a condition.
It's a check to see
if the number that we're currently looping in through list1
exists in list2.
And this is a very short and succinct way
of achieving our goals,
but it is not the most straightforward to read
and to understand.
You might have to think about it a few times.
You can try expanding it out
into a full for loop and a full if statement.
But essentially, the first step is to loop
through each number in list1.
Second step is to check each
of those numbers against every item in list2.
And then we end up with the values that match
that criteria and we add it into our result
and we create a new list using those results.
Now, the final thing to remember is that in order
for the result that's printed to match the tests,
the result has to be a list of integers.
So the final step before adding our num
into the list is to convert it into an integer.
And we do that at the first part of our list comprehension.
And now with these few lines of code,
we would've achieved the goals of the challenge
and we would end up with a result that passes all the tests.
So this is the point,
if you need to go back to fix your solution,
then it's time to go and do that.
If you manage to achieve everything,
then congratulations, a huge pat on the back.
This is not easy. I know.
And once you're happy with the code,
then proceed on to the next lesson.