5
votes

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.

9
the only thing i can think of is using a for loop in the function where the range of the input will be tested to see if it's divisible by 2 evenly. - captain_credible
You should look up modular division will help you a ton. Also, the "create an empty list" is doable, but you may also look at filter. - g.d.d.c
@justin: This is totally fine. If you want more specific help, write some code and show us what does not work. - Björn Pollex
@g.d.d.c the performance on a list comprehension is better than that for a filter - Rafe Kettler
Please follow general question guidelines: state any special restrictions, show what you've tried so far, and ask about what specifically is confusing you. - Roger Pate

9 Answers

10
votes

"By hand":

def even_only(lst):
    evens = []
    for number in lst:
        if is_even(number):
            evens.append(number)
    return evens

Pythonic:

def even_only(iter):
    return [x for x in iter if is_even(x)]

Since it's homework, you can fill in the is_even function.

7
votes

Simplest way would be to do what you posted in a comment -- iterate through the input list to find digits evenly divisible by 2, and add them to the return list if so.

The list.append(x) function will help you add an item to a list.

Also as mentioned, look at using the modulo operation to determine if a number is divisible by 2...

2
votes

The best way to do this (as a beginner) is probably a comprehension list. Since this is a homework, I won't do it for you, but here is the syntax :

[x for x in your_list if (your condition)]

You just have to replace (your condition) with what fits well (basically, exactly what you described).

P.S. I know some people may say comprehension lists are a bit advanced for a beginner, but I think it is not a concept too hard to catch and extremely useful.

2
votes
>>> a = [1, 3, 6, 10, 15, 21, 28]
>>> b = [i for i in a if i%2 ==0 ]
>>> b
[6, 10, 28]
>>> a
[1, 3, 6, 10, 15, 21, 28]
2
votes
>>> even_only = lambda seq : [ x for x in seq if str(x)[-1] in "02468" ]
>>> even_only([1, 3, 6, 10, 15, 21, 28])
[6, 10, 28]
2
votes

Use the filter function to do this in a functional way:

>>> even_filter = lambda x: not x % 2
>>> result = filter(even_filter, [0, 1, 2, 3, 4])
>>> assert result == [0, 2, 4]

Edit: updated with the correct parity of zero per Vincent's comment.

1
votes

I recently had this issue and used:

list=[1,2,3,4,5,6] #whatever your list is, just a sample
evens=[x for x in list if np.mod(x,2)==0]
print evens

returns [2,4,6]

0
votes

for every element in list, if element of list modulo is 0 then number is even

initial_list =  [1,22,13,41,15,16,87]
even_list = [ x  for x in initial_list if  x % 2 == 0]
even_list
# [22, 16]
0
votes

Seems a bit late.But whats given below, works fine for me:

def even_list(*args):  
    # Returns the even numbers in the list  
    return [x for x in args if (x % 2 == 0)] 



even_list(1,2,3,4,5,6,7,8)  
[2, 4, 6, 8]