Jason T. Eyerly Jason T. Eyerly - 5 months ago 18
Python Question

Python - "'NoneType' object is not iterable" error

My instructions in a codecademy project are to


Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result. For example, purify([1,2,3]) should return [2]. Do not directly modify the list you are given as input; instead, return a new list with only the even numbers.


The code I have come up with is:

def purify(numbers):

pure = numbers

for num in pure:
if num % 2 != 0:
pure = pure.remove(num)

return pure


And the error is:


Oops, try again. Your function crashed on [1] as input because your function throws a "'NoneType' object is not iterable" error.


As I've come to understand it, this means something in my code is being returned as "None". Or there is no data there. I can't seem to find what's wrong with this short and simple code, unless it's in my if statement. Thanks in advance.

So I made the edit within the code to remove "pure = pure.remove(num)" and that solved the none issue. However, when running the code it still fails the input of [4,5,5,4] and returns [4,5,4].

New Code:

def purify(numbers):

pure = numbers

for num in pure:
if num % 2 != 0:
pure.remove(num)

return pure

hlt hlt
Answer

The first problem is that you change the list while iterating over it. DO NOT change lists while you are iterating over them. You will miss out on some of the values in the loop.

The second problem is that list.remove acts in-place and returns None. Thus, when you do pure = pure.remove(num), you set pure to None, which is no longer iterable, which in turn is what causes the error. Instead, you can filter the list as follows

def purify(numbers):
    return [num for num in numbers if num % 2 == 0]
Comments