1
00:00:00,900 --> 00:00:05,580
Now the reason why we're learning about list comprehensions is because when we

2
00:00:05,580 --> 00:00:07,980
created all US states game,

3
00:00:08,490 --> 00:00:13,490
I realized that the code that we were writing could be a lot shorter if only we

4
00:00:14,340 --> 00:00:15,840
knew about list comprehensions.

5
00:00:16,260 --> 00:00:21,260
So I want you to head back over to your code for the US states game and open it

6
00:00:21,930 --> 00:00:25,230
up. And I want you to look at this if statement.

7
00:00:25,770 --> 00:00:28,560
So when the user types exit,

8
00:00:29,070 --> 00:00:34,070
we create a new list of missing states in order to save it to a CSV file.

9
00:00:36,570 --> 00:00:38,190
I want you to change this code

10
00:00:38,220 --> 00:00:42,690
using your new knowledge of this comprehension and see if you can cut down this

11
00:00:42,690 --> 00:00:44,820
code by three or four lines.

12
00:00:45,660 --> 00:00:48,300
Pause the video and complete this challenge now.

13
00:00:51,510 --> 00:00:55,860
All right. So instead of all of this, creating a new empty missing state

14
00:00:55,860 --> 00:01:00,570
and then going through a for loop, we can simply do this in one line. So again,

15
00:01:00,570 --> 00:01:05,570
it's going to be called missing_states and this is going to be a new list,

16
00:01:06,330 --> 00:01:10,320
but this time it's going to be created using our list comprehension;

17
00:01:10,830 --> 00:01:13,950
new item for item in list

18
00:01:14,790 --> 00:01:15,900
if test.

19
00:01:16,620 --> 00:01:20,850
The list in this case that we're looping through is all of our states.

20
00:01:21,210 --> 00:01:23,910
So we're gonna replace that with all states. Now,

21
00:01:23,970 --> 00:01:27,390
each of the items is basically a state in that list.

22
00:01:28,020 --> 00:01:33,020
And the test that we're going to make is to see well if the state is not in the

23
00:01:35,250 --> 00:01:39,270
guessed_states which has states added every time

24
00:01:39,270 --> 00:01:41,010
the user makes a correct guess

25
00:01:41,400 --> 00:01:45,330
so we can basically take this part of our previous code and put it here.

26
00:01:45,870 --> 00:01:49,470
And in that case, then we're going to add this particular state

27
00:01:49,530 --> 00:01:53,760
which pass this test into this new list.

28
00:01:54,270 --> 00:01:55,380
This one line

29
00:01:55,440 --> 00:02:00,440
basically will replace all four of these lines and it cuts down dramatically

30
00:02:01,320 --> 00:02:02,340
on the amount of code.

31
00:02:02,820 --> 00:02:07,620
And it reads relatively well as well. Add a state to this new list

32
00:02:08,009 --> 00:02:11,130
if we loop through all the states in the list of states

33
00:02:11,430 --> 00:02:14,520
and if that state is not in this list of guessed

34
00:02:14,520 --> 00:02:16,860
states. List

35
00:02:16,860 --> 00:02:21,860
comprehensions are super popular with Python developers and I hope through some

36
00:02:22,500 --> 00:02:23,580
of these exercises

37
00:02:23,580 --> 00:02:28,580
you can see why, just in terms of the sheer amount of code that it cuts down on

38
00:02:28,980 --> 00:02:32,520
and how much it simplifies things. Now,

39
00:02:32,580 --> 00:02:34,500
in addition to list comprehensions,

40
00:02:34,800 --> 00:02:39,660
we can also do comprehensions with dictionaries. So in the next lesson,

41
00:02:39,780 --> 00:02:40,980
that's what we're going to be looking at.

