I have a model which outputs a pairs of floats in a list, i.e [a,b] then [c,d] etc, but depending on even/odd datasets, will sometimes be a single float [e]. I would like to capture all of these floats when they're generated by appending them into a single list as [a,b,c,d,e].
I've had some partial success using append/extend, but nothing completely resolves my issue, as I keep running into a 'float' object is not iterable problem.
For example, this code will generate a new list [1.0, 3.0, 5.0, 7.0] but won't append the final z_ being 9.0, throwing a not iterable error.
information = []
blah = [[1.0, 3.0], [5.0, 7.0], 9.0]
for z in blah:
for z_ in z:
information.append(z_)
Similarly, using extend doesn't seem to work either
information = []
blah = [[1.0, 3.0], [5.0, 7.0], 9.0]
for z in blah:
for z_ in z:
information.extend(z_)
And using simpler version below runs, but leaves me with grouped a nested list [[1.0, 3.0], [5.0, 7.0], 9.0] which I was trying to avoid in the first place.
information = []
blah = [[1.0, 3.0], [5.0, 7.0], 9.0]
for z in blah:
information.append(z)
I've tried this approach as well
flat_list = [item for sublist in blah for item in sublist]
which still yields a float not iterable error.
This must be easy, but can't figure it out! Was hoping to avoid importing a new package like itertools for this one problem.