Welcome to part 5 of the intermediate Python programming tutorial series. In this part, we're going to talk more about list comprehension and generators.
Let's show a more realistic use case for generators and list comprehension:
Generator expression with a function:
input_list = [5,6,2,1,6,7,10,12] def div_by_five(num): if num % 5 == 0: return True else: return False xyz = (i for i in input_list if div_by_five(i)) print(list(xyz))
What we're doing is going through input_list
, and seeing if the number is divisble by five. If it is, then we're putting it in a new list. With the generator, we didn't copy input_list, we went through it one step at a time, and then only populated the new list if the number was indeed divisible by five. Let's do this with list comprehension:
xyz = [i for i in input_list if div_by_five(i)] print(xyz)
Before we leave this basic example, let's express a few more examples. For now, I will just do it with list comprehension, but you could use this logic with either.
You don't have to have an if
statement or a function to be at the end, you could do:
[print(i) for i in range(5)]
0 1 2 3 4
You could have any function in place of the print(i)
.
You can also embed:
[[print(i,ii) for ii in range(3)] for i in range(5)]
These are a bit more difficult to write out on your first attempts. You can build these by writing out the for loops regularly:
for i in range(5): for ii in range(3): print(i,ii)
Then, start from the end:
for ii in range(3): print(i,ii)
Do a list comprehension just for that:
[print(i,ii) for ii in range(3)]
Then do the next part (for i in range(5)
), so it'd be something like:
[... for i in range(5)]]
What goes in the ...?
the first one!
[[print(i,ii) for ii in range(3)] for i in range(5)]
You can just keep on embedding these things, and get pretty crazy. If you do find yourself embedding a bunch of these, you're *probably* doing something wrong, and probably should instead be doing a zip, enumerate, or something else, which we'll talk about soon.
We also have a fair amount more to cover with generators, but we'll have to come back to them.
For now, we're going to talk about the timeit
module in the next tutorial, which can help us to actually weigh the values of speed vs processing, as well as understand the main differences between a generator and list comprehension.