squares, twos, odds

Let us show you some other list comprehension examples:

Example #1:

squares = [x ** 2 for x in range(10)]

The snippet produces a ten-element list filled with squares of ten integer numbers starting from zero (0, 1, 4, 9, 16, 25, 36, 49, 64, 81)

Example #2:

twos = [2 ** i for i in range(8)]

The snippet creates an eight-element array containing the first eight powers of two (1, 2, 4, 8, 16, 32, 64, 128)

Example #3:

odds = [x for x in squares if x % 2 != 0 ]

The snippet makes a list with only the odd elements of the squares list.

Last updated