+3 votes
in Programming Languages by (3.0k 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

0 votes
by (28.9k points)

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