TypeError: "float" object is not iterable, Python list
I am writing a program in Python and trying to expand the list as such:
spectrum_mass[second] = [1.0, 2.0, 3.0]
spectrum_intensity[second] = [4.0, 5.0, 6.0]
spectrum_mass[first] = [1.0, 34.0, 35.0]
spectrum_intensity[second] = [7.0, 8.0, 9.0]
for i in spectrum_mass[second]:
if i not in spectrum_mass[first]:
spectrum_intensity[first].extend(spectrum_intensity[second][spectrum_mass[second].index(i)])
spectrum_mass[first].extend(i)
However, when I try to do this, I get TypeError: 'float' object is not iterable
on line 3.
To be clear, spectrum_mass[second]
is a list (that is, in a dictionary, the second and first are keys), like and spectrum_intensity[first]
, spectrum_intensity[second]
and spectrum_mass[second]
. All lists contain floats.
source to share
I am guessing the problem is with the line -
spectrum_intensity[first].extend(spectrum_intensity[second][spectrum_mass[second].index(i)])
Function extend()
expects an iterable, but you are trying to give it a float. The same behavior in a very small example -
>>> l = [1,2]
>>> l.extend(1.2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object is not iterable
Instead, you want to use .append()
-
spectrum_intensity[first].append(spectrum_intensity[second][spectrum_mass[second].index(i)])
Similar problem on next line, use append()
instead extend()
for -
spectrum_mass[first].extend(i)
source to share