write a comprehension that creates a list composed of even integers between 5 and 85 in reverse order in python

You can use the following list comprehension to create a list composed of even integers between 5 and 85 in reverse order in Python:

main.py
even_nums = [num for num in range(86, 4, -2) if num % 2 == 0]
62 chars
2 lines

This list comprehension starts with 86, which is the first even number greater than 85, and counts down by two until it reaches 6, which is the last even number greater than 4. It then filters out any odd numbers, and returns the resulting list in reverse order.

You can print the resulting list to check if it's correct:

main.py
print(even_nums)
# Output: [84, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6]
186 chars
3 lines

This will print a list of even numbers in reverse order between 5 and 85.

related categories

gistlibby LogSnag