when coding you spend a lot of time
making lists
in many languages this can be tedious
create an empty list set up a for loop
then add the items to the list one by
one
python cares about your sanity and gives
you a tool to simplify this process list
comprehensions
in most cases list comprehensions let
you construct a list in a single line of
code
it's now time for python to shine and
save time with a single line
we will cover many examples of list
comprehensions but first let's talk
about them generally
in python lists are a collection of data
surrounded by brackets and the elements
are separated by comments
a list comprehension is also surrounded
by brackets but instead of a list of
data inside you enter an expression
followed by for loops and if clauses
here is the most basic form for a list
comprehension
the first expression generates the
elements in the list and you follow this
with a for loop over some collection of
data
this will evaluate the expression for
every item in the collection
if you only want to include the
expression for certain pieces of data
you can add on an if clause after the
for loop
the expression will be added to the list
only if the if clause is true
you can even have more than one if
clause and the expression is in the list
only if all the if clauses are true
and you can even loop over more than one
collection
let's now see some examples
for our first example let's create a
list of the squares of the first 100
positive integers let's first do this
without list comprehensions
to begin you might create an empty list
called squares
next you would loop over the first 100
positive integers you would then append
the square of i to the list squares
don't forget that an exponent in python
is represented by double asterisks
why oh why do they not use the
intergalactic mathematical notation for
exponents
to see that this works print the list
squares
let's do this once more using list
comprehensions
we will call the list comprehension
squares 2.
open with a bracket then first type the
expression for each term in the list
finally enter the desired for loop
if you print this you get the exact same
list but we only needed one line of code
instead of three
let's now look at a slightly more
complex example
we'll create a list of remainders when
you divide the first 100 squares by five
to find the remainder when you divide by
five use the percent operator
in mathematics this is the same as
taking the square mod 5.
if you print the list you'll see there
are only three perfect squares mod 5 0 1
and 4.
let's do this again but look at the
remainders when you divide the squares
by 11.
we see the perfect squares mod 11 are 0
1 3 4 5 9.
this example shows you that the
expressions in the list comprehension
can be complex
by the way if you look at the remainders
when you divide by a prime number p
you'll notice an interesting pattern
the number of remainders is p plus one
divided by two
the problem of finding which numbers
appear in the list is a complex puzzle
from number theory known as quadratic
reciprocity and was first proved by
gauss
next let's create a list comprehension
that has an if clause
suppose we have a list of movies and we
want to find those movies that start
with the letter g
let's see how to do this with and
without list comprehensions
if you were not using list
comprehensions you'd start by making an
empty list
next loop over the list of movies
we can use the startswith method to see
if the title starts with the letter g if
it does then append it to our list
print the list to make sure that it
worked
but this four line routine can be done
in a single line with a list
comprehension
the expression we want to appear in our
list is simply the title next loop over
the movies
but also check that the title starts
with the letter g
print and observe
we get the same answer with a single
line of code
let's complicate this example a bit more
suppose our list of movies is a list of
tuples containing both the title of the
movie and the year it was released
what if we want a list of the titles of
all movies that were released before the
year 2000
how would you do this using list
comprehensions
as before we want our list to only
contain the titles but this time when we
write the for loop each element is a
tuple
next we select the movies released
before 2000 using an if clause on the
year
if you print the list you can see that
it worked
in this example the if block used the
year but the year was not included in
the list
only the title is included
let's see a mathematical example
suppose you use a list to represent a
vector how would you perform scalar
multiplication on this vector
that is what if we want to multiply each
number by four
you might be tempted to try four times v
but look what happens
this is
unusual what happened here is four times
v is the same as v plus v plus v plus v
and in python if you add two lists it
concatenates them rather than adding
them component wise
for example if you add 2 4 6 and 1 3 you
get the list 2 4 6 1 3
so 4 times v is just a list containing 4
copies of v
this is not what we want
we can achieve scalar multiplication
with a list comprehension
just make a list comprehension where you
multiply each component by four
if you print this vector you can see we
get the desired result
for our final example let's use list
comprehensions to compute the cartesian
product of sets
the cartesian product is named after the
french scholar rene descartes
recall that if you have two sets a and b
then the cartesian product is the set of
pairs where the first component is in a
and the second component is from b
you can write this mathematically like
this
for example if a is the set 1 3 and b is
the set x y then the cartesian product
is the set of pairs 1x 1y 3x and 3y
let's use list comprehensions to compute
the cartesian product of two sets
let a be the list of odd integers 1 3 5
and 7.
let b be the list of even integers 2 4 6
and 8.
to compute the cartesian product create
a list comprehension where each term is
the tuple a b
next write a for loop over the elements
of a
and then a for loop over the elements of
b
if you print the product you can see the
list contains all 16 possible pairs
using this technique you can even
compute the cartesian product of three
or more sets
with list comprehensions you can build
complex lists using a single line of
code and with socratica you can learn
complex topics in a single video
so please append yourself to our list of
subscribers
and if you are in a position to help
financially we would be honored to add
you to our list of patrons
now go forth and code compactly
you