Solution Review: Lists of Even and Odd Numbers

This lesson gives a detailed review of how to print the list of even and odd numbers using the list comprehension.

Solution 1:​ List Comprehension With Predicate

As we have already seen in the previous exercise, list comprehensions allow for in-place list creation using a range that mathematical operations can be done on; this solution uses the same approach.

  • Use a list comprehension l1 that iterates over a range of 0-21 and puts an even number in the list if the value is divisible by 2 (using modulus operator). Additionally, use a list comprehension l2 that iterates over a range of 0-21 and puts an odd number in the list if the value is not divisible by 2.

Get hands-on with 1200+ tech skills courses.