Home Python How to Fix Non-Type object is not iterable

How to Fix Non-Type object is not iterable

0
How to Fix Non-Type object is not iterable

If you are still working on python so, most of the time type errors come so, in this article, we will fix non-type object is not iterable

Contents

Python Type Error

With Python, you can possibly emphasize an object if that item has a value.

This is on the grounds that iterable objects possibly have a next thing that can be gotten to if their worth isn’t equivalent to None.

In the event that you attempt to repeat over a None object, you experience the TypeError: ‘NoneType’ object isn’t an iterable error.

Type Error: Fix non-Type Object is not iterable:

For an item to be iterable, it should contain a worth.

A None value isn’t iterable on the grounds that it doesn’t contain any items.

None addresses an invalid worth. There is a contrast between a None item and avoid iterable.

This error isn’t raised on the off chance that you have any unfilled rundown or a string.

This is on the grounds that rundowns and strings have an iterable information type.

At the point when the Python translator experiences an unfilled show, it doesn’t repeat over it on the grounds that there are no qualities.

Python can’t emphasize over a None worth so the mediator returns a mistake.

This mistake is regular when you proclaim a capacity and neglect to return a worth.

Example

We should compose a program that takes a list of understudy names and sift through those that start with “E”.

We’ll print those qualities to the control center.

Start by characterizing a capacity that channels out the understudies’ names:


def filter_students(class_names):

new_class_names = []

for c in class_names:

if c.startswith(“E”):

new_class_names.append©

This capacity loop through each thing in the “class_names” list utilizing a for circle.

For everything, our circle checks if the thing starts with the letter “E”.

It does, that name is added to the “new_class_names” list.

Then, compose a function that goes through our new rundown and prints out each worth to the control center:


def show_students(class_names):

for c in class_names:

print(c)

Here, we proclaim a rundown of understudies through which our program should look.

We pass this rundown of understudies through our filter_students work:


students = [“Elena”, “Peter”, “Chad”, “Sam”]

students_e_name = filter_students(students)

This code executes the filter_students work which tracks down every one of the understudies whose names start with “E”.

The list of students whose names start with “E” call students_e_name.

Then, we call our show_students capacity to show the new rundown of students:

Let’s run the code:


show_students(students_e_name)

Traceback (most recent call last):

File “main.py”, line 14, in <module>

Show_students(students_e_name)File “main.py”, line 8, in show_students
for c in class_names:

TypeError: ‘NoneType’ object is not iterable

Solution

At the point when we attempt to repeat over the variable class_names in the show_students work, our code identifies a None worth and raises a mistake.

This is on the grounds that the worth we have passed as “class_names” is None.

This error is caused on the grounds that our filter_students work doesn’t return a worth.

At the point when we dole out the aftereffect of the filter_students capacity to the variable students_e_name, the worth None is set.


To settle this error, we need to return worth in our filter_students work
def filter_students(class_names):
new_class_names = []
for c in class_names:
if c.startswith(“E”):
new_class_names.append©
# We have added a return statement here
return new_class_names

def show_students(class_names):
for c in class_names:
print(c)

students = [“Elena”, “Peter”, “Chad”, “Sam”]
students_e_name = filter_students(students)

show_students(students_e_name)
# Let’s run our code to see if it works:

OutPut

1Elena

Our code now successfully prints out the names of the students whose names begin with “E”.

To tackle this mistake, ensure that any qualities that you attempt to repeat over have been allocated an iterable article, similar to a string or a rundown.

In our model, we neglected to add a “return” explanation to a capacity Which made the capacity return None rather than a list.

Read More: How to replace characters in string Python?

nonetype’ object is not an iterable list

This is a common error message that pops up when you try to do something with a list or an iterator that isn’t actually a list or an iterator. The most common reason this happens is that you’re trying to use square brackets [] instead of parentheses () on a function call. So make sure you’re using the correct brackets for the type of thing you’re trying to call.

Argument of type ‘nonetype’ is not iterable Python

Iterating over None is an error in Python. Asking for the iterable attribute of None will result in an error.

Change your code to not ask for the iterable attribute of None and instead use some other way to test for whether or not a variable is None.

For example, you could test if a variable is None by using the ‘is’ keyword like this:


if my_var is None:
# do something
else:
# do something else

LEAVE A REPLY

Please enter your comment!
Please enter your name here