What's more, just as we can nest logic in our inline code, we can nest logic in a comprehension. This is where is gets wonky real fast so keep your production designs as real and maintainable as you can, but never be afraid to stretch the envelope for experimentation of one-off code situations where you just want a solution real quick.
Say we want a list of just the even numbers that are divisible by 3. Do we build a list of even numbers then trial divide by 3? Or do will build a list by 3's and trial divide by 2?
It looks to me that we could do either but our comprehensions will look a bit different from one another.
by_threes = [ x for x in range(100) if x % 3 == 0 ]
Now I see this is a terrible example. If all we want is a list by threes,
by_threes = range(3, 100, 3)
will give us that. That means we can just modify the range in your example and get the achieved result. But let's work with the exaggerated approach anyway, for the fun of it.
even_by_three = [y for y in [x for x in range(100) if x % 3 == 0] if y % 2 == 0]
Cool stuff, eh?
range(100) as being an actual list that is supplied to our program with no particular order or size. That 's where the above comprehension would prove useful because there is no predictibily of the inputs.