+3 votes
in Programming Languages by (40.5k points)

The following python code is giving the following error: TypeError: object of type 'filter' has no len().

I am using Python3 to run my code. How can I fix the error?

split = len(filter(lambda subset: subset.count() > 0, best_subsets_estimate)) == 1

1 Answer

+1 vote
by (353k points)
selected by
 
Best answer

In Python3, "filter" returns an Iterator, not a list. Hence your code is throwing error.

To fix the error, convert iterator to a list. Change your code to the following code and it should work.

split = len(list(filter(lambda subset: subset.count() > 0, best_subsets_estimate))) == 1


...