Is this list comprehension pythonic enough? [duplicate]

This question already has an answer here:

Let’s say I want to create a list of ints using Python that consists of the cubes of the numbers 1 through 10 only if the cube is evenly divisible by four.

I wrote this working line:

cube4 = [x ** 3 for x in range(1, 11) if (x ** 3) % 4 == 0]

My beef with this line of code is that it’s computing the cube of x twice. Is there more pythonic way to write this line? Or is this as good as it’ll get in a list comprehension?

Source: syntax

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.