0

I'm trying to define a function that returns a list of even integers from a list of overall integers

def print_even_numbers(n: list):
   '''Return a list of even numbers given a list of integers'''
   for x in list:
        if x % 2 == 0:
        return(x)

When I tried the code above, the error says that the type isn't iterable

Deer530
  • 73
  • 1
  • 1
  • 10

3 Answers3

2

list is the name of the list type. So you cannot iterate over a type. you should use n. Second, your return is indented wrong. It should be on the top function level, because return exits the function. Then you need to collect the result somewhere.

def print_even_numbers(n):
    '''Return a list of even numbers given a list of integers'''
    result = []
    for x in n:
        if x % 2 == 0:
           result.append(x)
    return result

This can be written in short by a list comprehension:

def print_even_numbers(n):
    '''Return a list of even numbers given a list of integers'''
    return [x for x in n if x % 2 == 0]
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • How are these two different? def print_even_numbers(n): '''Return a list of even numbers given a list of integers''' for x in n: if x % 2 == 0: return x vs. def print_even_numbers(n): '''Return a list of even numbers given a list of integers''' return [x for x in n if x % 2 == 0] – Deer530 Oct 10 '15 at 17:07
  • @Deer530: The two versions do the same, but the second one is shorter. – Daniel Oct 10 '15 at 17:30
  • When I tried the shortened version, it gave me a list of all the even integers with brackets around them (what I wanted), but for the first version, it only gave me back the first even number of the list. – Deer530 Oct 10 '15 at 17:34
  • Did you indent the function *exactly* as the example shows? What indentation level the `return` statement is at is *very* important so as to not end the loop before it is finished. – Dan Lowe Oct 10 '15 at 18:43
1
  • Wrong python syntax:

    def print_even_numbers(n: list):
    
  • You don't need brackets:

    return(x)
    
  • Wrong indentation. And wrong condition. (And don't use reserved python words such a list for your own variables.

    for x in list:
    

Summarize:

def print_even_numbers(n):
    '''Return a list of even numbers given a list of integers'''
    result = []
    for x in n:
        if x % 2 == 0:
            result.append(x)
    return result

print print_even_numbers(range(10))
>>> [0, 2, 4, 6, 8]

And finally more pythonic way is to use yield to implement desired behaviour:

def gen_even_numbers(n):
    for x in n:
        if x % 2 == 0:
            yield x

print list(gen_even_numbers(range(10)))
>>> [0, 2, 4, 6, 8]
xiº
  • 4,605
  • 3
  • 28
  • 39
  • I tried this code, but when I gave it a list (e.g. [1, 2, 3, 4]), it only gives me back the first even integer. Why is this? – Deer530 Oct 10 '15 at 17:00
0

you can use the filter built-in

def print_even_numbers(lst):
    return list(filter(lambda x: not x%2, lst))

Note: on python 2 filter returns a list already so there is no need to convert it

def print_even_numbers(lst):
    return filter(lambda x: not x%2, lst)

By the way, the function is named print_even_numbers but it does not print anything ;)

Pynchia
  • 10,996
  • 5
  • 34
  • 43