- [Instructor] In this video,
we're going to introduce a Python feature
known as list comprehensions
which are an incredibly expressive
and concise way of defining new list objects.
Now up until this point when we've created lists,
we've written expressions
like list1 = empty square brackets to create a list
or we've predefined the set of values in a list.
In this case, we might then go and append items to the list
so for example here's a little for loop
in which we're going to walk through the values
from one up to but not including six,
again one through five,
and one item at a time we're going to add that item to list1
with the append method and then of course we can see
that list1 does indeed contain those values.
Well, it turns out that list comprehensions
are another way of performing the same task
and they can replace most for loops
like the one that you see in snippet two here.
So here is an equivalent list comprehension.
A list comprehension looks a little bit like what you do
when you create a list in the first place.
It's something that's defined
inside a set of square brackets
but it consists of an expression, a for keyword.
In fact, it looks very much like
the first line of a for statement.
And basically what we're saying here is
for every item in the range one up to but not including six,
in this case we would like to take that item
and add it to this new list that we're going to create.
So if I go ahead and execute that and then evaluate list2,
you can see that we get the exact same result
and notice that we did not have to call
the append method in this case.
What happened here is the comprehension
automatically iterated through
the range of elements we specified.
And for every item in that range,
it simply did what we said to do
to the left of the for keyword which is to include that item
in the new list that we're creating.
Now list comprehensions are one of Python's
functional style programming capabilities.
It's kind of a declarative way of saying
what you would like in this case to place into a list.
And another common functional style programming technique
is called mapping and mapping is the case
where you take a sequence of values
and for every value in the sequence
you apply an operation to it to produce a new value.
So if you have 10 items in your sequence,
the map sequence will also have 10 items.
If you have five items in your sequence,
the map sequence would also have five items.
So let's go ahead and demonstrate
another list comprehension here.
This time we're going to do a list comprehension
in which we have a little bit more complex expression
to the left of the for keyword.
So let's focus on the for part of this first
and once again you can see for every item
in the range one up to but not including six,
in the range one through five,
we would like to take that item and cube its value,
that is map it to the cube of itself.
So let's execute that list comprehension
and see what we get for results.
And indeed, you can see that the values
from one through five were cubed.
So two times two times two is eight.
Three times three times three is 27, etc etc.
So that's one common style of functional
style programming technique, the mapping operation.
Another common functional style programming technique
is filtering where you look at each element in a sequence
and you include it in the result
only if some condition is true.
Well, it turns out list comprehensions
have a way of doing that also
and here we have another list comprehension,
again the comprehension is defined inside square brackets.
And what we're saying in this case
is we would like to include each item
for the items in the range one up to but not including 11,
but we'd only like to include those items
if the given item is divisible by two.
So item % 2 says give me the remainder
after dividing by two.
If that value is equal to zero, it's an even number.
If that value is not equal to zero, it's an odd number.
So what we're basically saying is in this range of elements,
we only want to include the elements in list4
if they are even numbers.
So let's confirm that indeed that is the case
and you can see even though we counted from one through 10,
only two, four, six, eight and 10 were included
in the final result.
Now so far we've looked only at ranges of values
being used in our list comprehensions
but the item to the right of the in keyword
just like in a regular for loop
can be any sequence of elements
over which you'd like to iterate to help produce the values
that will be included in the list.
So let's go ahead and define a list of strings called colors
and you can see here we have all lowercase strings so far.
And let's assume that what we'd like to do
is to create a separate list, a copy of colors,
in which we have the uppercase versions
of each of these strings.
Well, that's a mapping operation.
Every element will be mapped
to the uppercase version of that element
and placed into a new list that we're creating
via a list comprehension.
So let me go ahead and paste in a snippet of code
that demonstrates that concept.
So in this case for every item in the list called colors,
we would like to take that item and invoke its upper method
which is going to give us back
the uppercase version of that string
and that new string is what will then get inserted
into the list that is being produced
by this list comprehension.
So at this point we can go and evaluate colors2
and we can see indeed we have the same number of elements
as we had up above.
They're all in uppercase letters.
And separately, just to confirm,
we can go ahead and evaluate colors
to ensure that it was not modified
by the list comprehension performing the mapping operation.
So you can see that indeed
it still contains the same values.