1 00:00:00,560 --> 00:00:01,393 - [Instructor] In this video, 2 00:00:01,393 --> 00:00:04,340 we're going to introduce a Python feature 3 00:00:04,340 --> 00:00:06,800 known as list comprehensions 4 00:00:06,800 --> 00:00:10,270 which are an incredibly expressive 5 00:00:10,270 --> 00:00:15,060 and concise way of defining new list objects. 6 00:00:15,060 --> 00:00:18,340 Now up until this point when we've created lists, 7 00:00:18,340 --> 00:00:19,980 we've written expressions 8 00:00:19,980 --> 00:00:24,750 like list1 = empty square brackets to create a list 9 00:00:24,750 --> 00:00:28,190 or we've predefined the set of values in a list. 10 00:00:28,190 --> 00:00:32,260 In this case, we might then go and append items to the list 11 00:00:32,260 --> 00:00:35,390 so for example here's a little for loop 12 00:00:35,390 --> 00:00:37,670 in which we're going to walk through the values 13 00:00:37,670 --> 00:00:40,140 from one up to but not including six, 14 00:00:40,140 --> 00:00:41,630 again one through five, 15 00:00:41,630 --> 00:00:46,240 and one item at a time we're going to add that item to list1 16 00:00:46,240 --> 00:00:49,040 with the append method and then of course we can see 17 00:00:49,040 --> 00:00:52,840 that list1 does indeed contain those values. 18 00:00:52,840 --> 00:00:55,760 Well, it turns out that list comprehensions 19 00:00:55,760 --> 00:00:59,900 are another way of performing the same task 20 00:00:59,900 --> 00:01:03,610 and they can replace most for loops 21 00:01:03,610 --> 00:01:06,740 like the one that you see in snippet two here. 22 00:01:06,740 --> 00:01:10,380 So here is an equivalent list comprehension. 23 00:01:10,380 --> 00:01:13,920 A list comprehension looks a little bit like what you do 24 00:01:13,920 --> 00:01:16,330 when you create a list in the first place. 25 00:01:16,330 --> 00:01:17,630 It's something that's defined 26 00:01:17,630 --> 00:01:19,670 inside a set of square brackets 27 00:01:19,670 --> 00:01:24,490 but it consists of an expression, a for keyword. 28 00:01:24,490 --> 00:01:26,010 In fact, it looks very much like 29 00:01:26,010 --> 00:01:27,920 the first line of a for statement. 30 00:01:27,920 --> 00:01:30,800 And basically what we're saying here is 31 00:01:30,800 --> 00:01:35,260 for every item in the range one up to but not including six, 32 00:01:35,260 --> 00:01:37,980 in this case we would like to take that item 33 00:01:37,980 --> 00:01:42,190 and add it to this new list that we're going to create. 34 00:01:42,190 --> 00:01:46,250 So if I go ahead and execute that and then evaluate list2, 35 00:01:46,250 --> 00:01:48,970 you can see that we get the exact same result 36 00:01:48,970 --> 00:01:51,000 and notice that we did not have to call 37 00:01:51,000 --> 00:01:53,040 the append method in this case. 38 00:01:53,040 --> 00:01:55,230 What happened here is the comprehension 39 00:01:55,230 --> 00:01:57,730 automatically iterated through 40 00:01:57,730 --> 00:01:59,960 the range of elements we specified. 41 00:01:59,960 --> 00:02:02,450 And for every item in that range, 42 00:02:02,450 --> 00:02:04,970 it simply did what we said to do 43 00:02:04,970 --> 00:02:08,970 to the left of the for keyword which is to include that item 44 00:02:08,970 --> 00:02:11,630 in the new list that we're creating. 45 00:02:11,630 --> 00:02:16,110 Now list comprehensions are one of Python's 46 00:02:16,110 --> 00:02:19,300 functional style programming capabilities. 47 00:02:19,300 --> 00:02:21,980 It's kind of a declarative way of saying 48 00:02:21,980 --> 00:02:25,450 what you would like in this case to place into a list. 49 00:02:25,450 --> 00:02:29,430 And another common functional style programming technique 50 00:02:29,430 --> 00:02:32,740 is called mapping and mapping is the case 51 00:02:32,740 --> 00:02:36,220 where you take a sequence of values 52 00:02:36,220 --> 00:02:38,020 and for every value in the sequence 53 00:02:38,020 --> 00:02:42,020 you apply an operation to it to produce a new value. 54 00:02:42,020 --> 00:02:44,060 So if you have 10 items in your sequence, 55 00:02:44,060 --> 00:02:46,930 the map sequence will also have 10 items. 56 00:02:46,930 --> 00:02:48,680 If you have five items in your sequence, 57 00:02:48,680 --> 00:02:52,160 the map sequence would also have five items. 58 00:02:52,160 --> 00:02:54,510 So let's go ahead and demonstrate 59 00:02:54,510 --> 00:02:57,870 another list comprehension here. 60 00:02:57,870 --> 00:03:00,690 This time we're going to do a list comprehension 61 00:03:00,690 --> 00:03:03,550 in which we have a little bit more complex expression 62 00:03:03,550 --> 00:03:05,750 to the left of the for keyword. 63 00:03:05,750 --> 00:03:09,100 So let's focus on the for part of this first 64 00:03:09,100 --> 00:03:11,390 and once again you can see for every item 65 00:03:11,390 --> 00:03:14,440 in the range one up to but not including six, 66 00:03:14,440 --> 00:03:16,050 in the range one through five, 67 00:03:16,050 --> 00:03:20,240 we would like to take that item and cube its value, 68 00:03:20,240 --> 00:03:23,610 that is map it to the cube of itself. 69 00:03:23,610 --> 00:03:26,580 So let's execute that list comprehension 70 00:03:26,580 --> 00:03:29,350 and see what we get for results. 71 00:03:29,350 --> 00:03:31,810 And indeed, you can see that the values 72 00:03:31,810 --> 00:03:33,540 from one through five were cubed. 73 00:03:33,540 --> 00:03:35,740 So two times two times two is eight. 74 00:03:35,740 --> 00:03:39,630 Three times three times three is 27, etc etc. 75 00:03:39,630 --> 00:03:43,070 So that's one common style of functional 76 00:03:43,070 --> 00:03:46,880 style programming technique, the mapping operation. 77 00:03:46,880 --> 00:03:50,940 Another common functional style programming technique 78 00:03:50,940 --> 00:03:55,470 is filtering where you look at each element in a sequence 79 00:03:55,470 --> 00:03:57,300 and you include it in the result 80 00:03:57,300 --> 00:04:00,560 only if some condition is true. 81 00:04:00,560 --> 00:04:02,630 Well, it turns out list comprehensions 82 00:04:02,630 --> 00:04:04,980 have a way of doing that also 83 00:04:04,980 --> 00:04:07,910 and here we have another list comprehension, 84 00:04:07,910 --> 00:04:11,710 again the comprehension is defined inside square brackets. 85 00:04:11,710 --> 00:04:13,700 And what we're saying in this case 86 00:04:13,700 --> 00:04:16,590 is we would like to include each item 87 00:04:16,590 --> 00:04:20,830 for the items in the range one up to but not including 11, 88 00:04:20,830 --> 00:04:23,900 but we'd only like to include those items 89 00:04:23,900 --> 00:04:27,900 if the given item is divisible by two. 90 00:04:27,900 --> 00:04:31,450 So item % 2 says give me the remainder 91 00:04:31,450 --> 00:04:33,020 after dividing by two. 92 00:04:33,020 --> 00:04:36,430 If that value is equal to zero, it's an even number. 93 00:04:36,430 --> 00:04:39,330 If that value is not equal to zero, it's an odd number. 94 00:04:39,330 --> 00:04:42,810 So what we're basically saying is in this range of elements, 95 00:04:42,810 --> 00:04:47,270 we only want to include the elements in list4 96 00:04:47,270 --> 00:04:49,630 if they are even numbers. 97 00:04:49,630 --> 00:04:52,580 So let's confirm that indeed that is the case 98 00:04:52,580 --> 00:04:55,910 and you can see even though we counted from one through 10, 99 00:04:55,910 --> 00:05:00,210 only two, four, six, eight and 10 were included 100 00:05:00,210 --> 00:05:02,240 in the final result. 101 00:05:02,240 --> 00:05:06,980 Now so far we've looked only at ranges of values 102 00:05:06,980 --> 00:05:09,920 being used in our list comprehensions 103 00:05:09,920 --> 00:05:12,910 but the item to the right of the in keyword 104 00:05:12,910 --> 00:05:15,150 just like in a regular for loop 105 00:05:15,150 --> 00:05:17,720 can be any sequence of elements 106 00:05:17,720 --> 00:05:21,700 over which you'd like to iterate to help produce the values 107 00:05:21,700 --> 00:05:24,060 that will be included in the list. 108 00:05:24,060 --> 00:05:28,730 So let's go ahead and define a list of strings called colors 109 00:05:28,730 --> 00:05:32,630 and you can see here we have all lowercase strings so far. 110 00:05:32,630 --> 00:05:35,300 And let's assume that what we'd like to do 111 00:05:35,300 --> 00:05:40,300 is to create a separate list, a copy of colors, 112 00:05:40,800 --> 00:05:43,810 in which we have the uppercase versions 113 00:05:43,810 --> 00:05:45,350 of each of these strings. 114 00:05:45,350 --> 00:05:47,510 Well, that's a mapping operation. 115 00:05:47,510 --> 00:05:49,320 Every element will be mapped 116 00:05:49,320 --> 00:05:52,170 to the uppercase version of that element 117 00:05:52,170 --> 00:05:55,760 and placed into a new list that we're creating 118 00:05:55,760 --> 00:05:57,620 via a list comprehension. 119 00:05:57,620 --> 00:06:00,430 So let me go ahead and paste in a snippet of code 120 00:06:00,430 --> 00:06:02,440 that demonstrates that concept. 121 00:06:02,440 --> 00:06:07,100 So in this case for every item in the list called colors, 122 00:06:07,100 --> 00:06:11,140 we would like to take that item and invoke its upper method 123 00:06:11,140 --> 00:06:12,440 which is going to give us back 124 00:06:12,440 --> 00:06:14,450 the uppercase version of that string 125 00:06:14,450 --> 00:06:18,200 and that new string is what will then get inserted 126 00:06:18,200 --> 00:06:20,860 into the list that is being produced 127 00:06:20,860 --> 00:06:22,800 by this list comprehension. 128 00:06:22,800 --> 00:06:26,420 So at this point we can go and evaluate colors2 129 00:06:26,420 --> 00:06:29,570 and we can see indeed we have the same number of elements 130 00:06:29,570 --> 00:06:31,040 as we had up above. 131 00:06:31,040 --> 00:06:32,920 They're all in uppercase letters. 132 00:06:32,920 --> 00:06:36,300 And separately, just to confirm, 133 00:06:36,300 --> 00:06:39,280 we can go ahead and evaluate colors 134 00:06:39,280 --> 00:06:42,090 to ensure that it was not modified 135 00:06:42,090 --> 00:06:46,240 by the list comprehension performing the mapping operation. 136 00:06:46,240 --> 00:06:47,850 So you can see that indeed 137 00:06:47,850 --> 00:06:50,233 it still contains the same values.