Python for Entrepreneurs Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: list comprehensions
0:01 Python has a great declarative way to process a set of items and either turn it into a list, a dictionary, a set or a generator.
0:10 Let's look at the list version through an example. Here we have some get_active_customers method,
0:17 maybe it goes to a database, maybe it just goes to some data structure, it doesn't really matter, but it comes back with an iterable set of users,
0:24 so we could loop over all of the users, using a "for...in" loop to find the users who have paid today and get their usernames and put that into a list.
0:34 So what we do is we create a list, some name paying usernames and we'd "for...in" over those to loop over all of them and then we do a test,
0:42 we'd say if that particular user's last purchase was today then append to their username to this paying usernames list.
0:51 And then in the end, we'd have a list, which is all the usernames of the customers you bought something from us today.
0:57 This would be an imperative users' search, an imperative style of programming, where you explicitly say all the steps,
1:03 let's see how we could do this instead with the list comprehension. Here you'll see many of the same elements,
1:10 and it looks like we are declaring a list, so [ ] in Python means declare an empty list, but there is stuff in the middle.
1:17 The way you read this you kind of got to piece it together, maybe top to bottom is not necessarily the best way to put this all together
1:23 but let's go top to bottom for a minute and then I'll pull out the pieces for you. So, we are going to get the name of the user,
1:28 and we are going to later introduce a variable called "u", which is the individual user for the set we are going through, so we'd say u.name,
1:34 that's like our projection that we want, and there is a "for...in" statement, like we had before, where we get the active customers
1:41 and we are going to process them, and then there is some kind of test whether or not that particular user should be in this set.
1:47 So, we set the source, that's going to be out get_active_customers and we are going to express that we are iterating over that for "u" in that set
1:56 and "u" declares the local variable that we are going to work with, we are going to filter on that with an "if" test,
2:01 and finally we are going to do some kind of projection, we could just say "u" to get all the users, here we want all the usernames
2:07 so we say u.name. Now, there are multiple structures like this in Python, we could have parenthesis that would generate a generator,
2:14 but as I said before, [ ] represents list, and so when you have the [ ] here,
2:19 you know what is going to come out is a list, and this is a list comprehension.
2:24 Once you get used to it, you'll find this style of programming is a little cleaner and a little more concise.
2:30 It's also different in another important way, because this can be just part of a larger expression,
2:35 this could be say in inline argument to a method you are calling. Or, you could chain it together with other comprehensions,
2:43 or other types of processing. The imperative style of programming required separate language structures that required their own blocks,
2:52 so you can't really compose "for...in" loops but you can compose these comprehensions
2:55 which makes then really useful in places the "for...in" loop wouldn't be.