Skip to content Skip to sidebar Skip to footer

Print The Even Numbers From A Given List

I'm starting with python and practicing a code to print even numbers out of a list. Sample List: [1, 2, 3, 4, 5, 6, 7, 8, 9] Code: def p_even(lst): for num in lst: if

Solution 1:

Yes, that code will return the first even number it finds, ceasing processing of the list when it does.

If you want all the even numbers, you can just use:

def p_even(lst):
    #       _____Construct new list_____
    #      /                            \
    return [x for x in lst if x % 2 == 0]
    #       \____________/ \___________/
    # from these elements   that meet this condition

(the comments are there for explanation only, they're probably not needed in any actual code you write).

This is called list comprehension in Python and is a powerful way (in this case) to create another list from your list by filtering certain elements.

To print the elements, you can either just print the returned list itself (the first line below) or print each element (the for loop below):

print(p_even(range(20))
for itm in p_even(range(20)):
    print(itm)

You can use an explicit loop for this, such as:

def p_even(lst):
    even_lst = []
    for itm in lst:
        if itm % 2 == 0:
            even_lst.append(itm)

But it's not really considered "pythonic". Python provides very expressive (and concise) ways to do this sort of action and, in my opinion, you would be better off learning them since they'll make your code much more readable and easy to maintain.


Solution 2:

This will do the job:

def p_even(lst):
    return [x for x in lst if not x % 2]

And here is some tests:

>> def p_even(lst):
...     return [x for x in lst if not x % 2]
... 
>>> p_even(range(100))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
>>> p_even(range(50))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
>>> p_even(range(103,120))
[104, 106, 108, 110, 112, 114, 116, 118]
>>> 

And here is another way using generators:

def p_even(lst):
    for x in lst:
        if not x % 2:
            yield x

And the corresponding usage:

>>> def p_even(lst):
...     for x in lst:
...         if not x % 2:
...             yield x
... 
>>> p_even(range(10))
<generator object p_even at 0x7fe0ff77e840>
>>> list(p_even(range(10)))
[0, 2, 4, 6, 8]
>>> 

Solution 3:

You can do this in a list comprehension like:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [x for x in list1 if x % 2 == 0]
print(list2)

Yielding:

[2, 4, 6, 8]

If you want to abstract this into a function it would look more like:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def get_evens(input_list):
    output = [x for x in input_list if x % 2 == 0]
    return output
print(get_evens(list1))

Arriving at the same result!


Solution 4:

Your return statement should be at the same indentation level as for. Also you need to save this list and return all even num if that is what you want to do. (Also assuming you want to do it with a loop approach, otherwise there are more ways to solve the problem.)

def p_even(lst):
    ret_list = []
    for num in lst:
        if num %2 == 0:
            ret_list.append(num)
     return ret_list

Solution 5:

You need to declare a list within p_even(). Within the for loop, add all the even numbers encountered to the list. In the end, outside the for-loop, return the list back.

def p_even(lst):
    even_num = []
    for num in lst:
        if num %2 == 0:
            even_num.append(num)
    return even_num

A shorter solution (might be slightly tougher for new python programmers to get at first shot) would be:

def p_even(lst):
    return [x for x in lst if x % 2 == 0]

This code essentially returns a list containing every value x in lst where x % 2 == 0; x is even.

Both the methods above work for a list like [1, 2, 3, 4, 5, 6, 7, 8, 9], returning [2, 4, 6, 8].


Post a Comment for "Print The Even Numbers From A Given List"