Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object of type 'map' has no len() in Python 3

I have a problem with Python 3. I got Python 2.7 code and at the moment I am trying to update it. I get the error:

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

at this part:

str(len(seed_candidates)) 

Before I initialized it like this:

seed_candidates = map(modify_word, wordlist) 

So, can someone explain me what I have to do?

(EDIT: Previously this code example was wrong because it used set instead of map. It has been updated now.)

like image 803
Sonius Avatar asked May 02 '16 12:05

Sonius


People also ask

What does object of type int has no len () mean?

The Python "TypeError: object of type 'int' has no len()" occurs when we pass an integer to the len() function. To solve the error, convert the integer to a string, e.g. len(str(my_int)) or correct the assignment and pass a sequence ( list , str , etc) to the len() function. Here is an example of how the error occurs.

Does Python have no Len?

The Python "TypeError: object of type 'function' has no len()" occurs when we pass a function without calling it to the len() function. To solve the error, make sure to call the function and pass the result to the len() function.

What is map object in Python?

Python map() function is used to apply a function on all the elements of specified iterable and return map object. Python map object is an iterator, so we can iterate over its elements. We can also convert map object to sequence objects such as list, tuple etc. using their factory functions.

How do I find the size of a map in Python?

int n = mymap. size(); Doc.


2 Answers

In Python 3, map returns a map object not a list:

>>> L = map(str, range(10)) >>> print(L) <map object at 0x101bda358> >>> print(len(L)) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: object of type 'map' has no len() 

You can convert it into a list then get the length from there:

>>> print(len(list(L))) 10 
like image 74
TerryA Avatar answered Sep 21 '22 02:09

TerryA


While the accepted answer might work for the OP, there are some things to learn here, because sometimes you can't find the length even with doing the OP's map(modify_word, wordlist) casted into list and checking the length with len(list(map(modify_word, wordlist))). You can't because sometimes the length is infinite.

For example let's consider the following generator that lazy calculate all the naturals:

def naturals():     num = 0     while True:         yield num         num +=1 

And let's say I want to get the square of each of those, that is,

doubles = map(lambda x: x**2, naturals()) 

Note that this is a completely legit use of map function, and will work, and will allow you to use next() function on the doubles variable:

>>> doubles = map(lambda x: x**2, naturals()) >>> next(doubles) 0 >>> next(doubles) 1 >>> next(doubles) 4 >>> next(doubles) 9 ... 

But, what if we try to cast it into a list? Obviously python can't know if we are trying to iterate through a never-ending iterator. So if we'll try to cast an instance of this mapObject to a list, python will try and keep trying and will get stuck on an infinite loop.

So when you cast to list, you should first make sure you know your map object will indeed yield finite number of elements.

like image 39
barshopen Avatar answered Sep 21 '22 02:09

barshopen