sodoesaburningbus sodoesaburningbus - 1 year ago 86
Python Question

Python -- list(some_float) fails but [some_float] works?

I have a case where a user passes a function a single floating-point value. While trying to put that value into a list for easier data handling later, I've discovered that I cannot make a list using

, but
does work. Python prints an error stating that "'float' object is not iterable."

My question for you wonderful people is why
works, but
does not. My understanding is that they produce identical results even if they are executed differently. I am using Python 3.4.3.

Answer Source

list(thing) doesn't mean "put thing in a list". It means "put thing's elements in a list". If you want to put a thing in a list, that's [thing].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download