List Comprehension || Python Tutorial || Learn Python Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 to 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 5 to find the remainder when you divide by 5 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 3 perfect squares mod 5 0 1 & 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 1 divided by 2 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 are not using list comprehensions you'd start by making an empty list next loop over the list of movies we can use the starts with method to see if the title starts with the letter G if it does then append it to a 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 on 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 4 you might be tempted to try 4 times V but look what happens this is unusual what happened here is 4 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 4 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 the account recall that if you have two sets a and B then the Cartesian product is the set of pairs where the first component is an A and the second component is from B you can write this mathematically like this for example if a is the set one three and B is the set X Y then the Cartesian product is the set of pairs 1 X 1 y 3 x + 3 y let's use list comprehensions to compute the Cartesian product of two sets let a be the list of odd integers 1 3 5 & 7 let B be the list of even integers 2 4 6 & 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 socratic ax you can learn complex topics in a single video so please upend 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
Info
Channel: Socratica
Views: 335,046
Rating: 4.9519987 out of 5
Keywords: Socratica, SocraticaCS, python, python programming, list comprehension, list comprehensions, learn python, socraticapython, how to program in python, programming in python, list comprehension python, python list comprehension, python tutorial, python tutorial for beginners
Id: AhSvKGTh28Q
Channel Id: undefined
Length: 7min 43sec (463 seconds)
Published: Sat May 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.