Python len() function reporting 'float' error when handling a list

Question:

I have a list of lists that I initiate as follows:

patches = [[]]

and then I append to main list by patches.append(list_of_x), and to the sub lists I append objects by patches[i].append(x).

When I call len(patches) I get error "’float’ object is not callable".

I did verify by type(patches) that Python still considers this a list.

The objects within the sub-lists patches[i] themselves have variety of different attributes, but I think that should be irrelevant (right?).

Any idea what kind of witchcraft could be happening?

Asked By: LuTze

||

Answers:

I think you might have defined a variable called len before that in your code. And this variable is probably a float, which, as the error says, you can’t call.

Answered By: Simon

don’t do patches = [[]]

do it patches = []
when you add, write
patches.append([x,y,x])

like this
patches.append([])

see

v = []
v.append([2,2,5,8,2])
v.append([6,6,8,8,7])
print(len(v))

2

Answered By: omer altaher
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.