Solution Review: Lists of Even and Odd Numbers
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.