How do I create a list and only extract or search out the even numbers in that list?
Create a function even_only(l)
that takes a list of integers as its only argument. The
function will return a new list containing all (and only) the elements of l which are evenly divisible by 2. The original list l shall remain unchanged.
For examples, even_only([1, 3, 6, 10, 15, 21, 28])
should return [6, 10, 28]
, and
even_only([1, 4, 9, 16, 25])
should return [4, 16]
.
Hint: Start by creating an empty list, and whenever you encounter an even number in it, add it to your list, then at the end, return your list.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…