close

[Solved] Python: TypeError: object of type ‘NoneType’ has no len()

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python: TypeError: object of type ‘NoneType’ has no len() in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python: TypeError: object of type ‘NoneType’ has no len() Error Occurs?

Today I get the following error Python: TypeError: object of type ‘NoneType’ has no len() in python.

How To Solve Python: TypeError: object of type ‘NoneType’ has no len() Error ?

  1. How To Solve Python: TypeError: object of type 'NoneType' has no len() Error ?

    To Solve Python: TypeError: object of type 'NoneType' has no len() Error You don't need to assign names to list or [] or anything else until you wish to use it.

  2. Python: TypeError: object of type 'NoneType' has no len()

    To Solve Python: TypeError: object of type 'NoneType' has no len() Error You don't need to assign names to list or [] or anything else until you wish to use it.

Solution 1

shuffle(names) is an in-place operation. Drop the assignment.

This function returns None and that’s why you have the error:

TypeError: object of type 'NoneType' has no len()

Solution 2

You don’t need to assign names to list or [] or anything else until you wish to use it.

It’s neater to use a list comprehension to make the list of names.

shuffle modifies the list you pass to it. It always returns None

If you are using a context manager (with ...) you don’t need to close the file explicitly

from random import shuffle
with open('names') as f:
    names = [name.rstrip() for name in f if not name.isspace()]
    shuffle(names)
assert len(names) > 100

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read