sodoesaburningbus sodoesaburningbus - 3 months ago 7
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

list(some_float)
, but
[some_float]
does work. Python prints an error stating that "'float' object is not iterable."

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

Answer

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