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?
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.
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
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?
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.
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