WEBVTT

0
00:00.900 --> 00:05.580
Now the reason why we're learning about list comprehensions is because when we

1
00:05.580 --> 00:07.980
created all US states game,

2
00:08.490 --> 00:13.490
I realized that the code that we were writing could be a lot shorter if only we

3
00:14.340 --> 00:15.840
knew about list comprehensions.

4
00:16.260 --> 00:21.260
So I want you to head back over to your code for the US states game and open it

5
00:21.930 --> 00:25.230
up. And I want you to look at this if statement.

6
00:25.770 --> 00:28.560
So when the user types exit,

7
00:29.070 --> 00:34.070
we create a new list of missing states in order to save it to a CSV file.

8
00:36.570 --> 00:38.190
I want you to change this code

9
00:38.220 --> 00:42.690
using your new knowledge of this comprehension and see if you can cut down this

10
00:42.690 --> 00:44.820
code by three or four lines.

11
00:45.660 --> 00:48.300
Pause the video and complete this challenge now.

12
00:51.510 --> 00:55.860
All right. So instead of all of this, creating a new empty missing state

13
00:55.860 --> 01:00.570
and then going through a for loop, we can simply do this in one line. So again,

14
01:00.570 --> 01:05.570
it's going to be called missing_states and this is going to be a new list,

15
01:06.330 --> 01:10.320
but this time it's going to be created using our list comprehension;

16
01:10.830 --> 01:13.950
new item for item in list

17
01:14.790 --> 01:15.900
if test.

18
01:16.620 --> 01:20.850
The list in this case that we're looping through is all of our states.

19
01:21.210 --> 01:23.910
So we're gonna replace that with all states. Now,

20
01:23.970 --> 01:27.390
each of the items is basically a state in that list.

21
01:28.020 --> 01:33.020
And the test that we're going to make is to see well if the state is not in the

22
01:35.250 --> 01:39.270
guessed_states which has states added every time

23
01:39.270 --> 01:41.010
the user makes a correct guess

24
01:41.400 --> 01:45.330
so we can basically take this part of our previous code and put it here.

25
01:45.870 --> 01:49.470
And in that case, then we're going to add this particular state

26
01:49.530 --> 01:53.760
which pass this test into this new list.

27
01:54.270 --> 01:55.380
This one line

28
01:55.440 --> 02:00.440
basically will replace all four of these lines and it cuts down dramatically

29
02:01.320 --> 02:02.340
on the amount of code.

30
02:02.820 --> 02:07.620
And it reads relatively well as well. Add a state to this new list

31
02:08.010 --> 02:11.130
if we loop through all the states in the list of states

32
02:11.430 --> 02:14.520
and if that state is not in this list of guessed

33
02:14.520 --> 02:16.860
states. List

34
02:16.860 --> 02:21.860
comprehensions are super popular with Python developers and I hope through some

35
02:22.500 --> 02:23.580
of these exercises

36
02:23.580 --> 02:28.580
you can see why, just in terms of the sheer amount of code that it cuts down on

37
02:28.980 --> 02:32.520
and how much it simplifies things. Now,

38
02:32.580 --> 02:34.500
in addition to list comprehensions,

39
02:34.800 --> 02:39.660
we can also do comprehensions with dictionaries. So in the next lesson,

40
02:39.780 --> 02:40.980
that's what we're going to be looking at.