Search code examples
pythonpython-3.xfloating-pointreturn

How to return some float values with zip() in Python function? TypeError: 'float' object is not iterable


I wrote a simple function that wants to return more than 1 float values at the same time

def test_zip_float():
    return zip(1.234, 3.456)

print(test_zip_float())

It results in:

TypeError: 'float' object is not iterable

I can rewrite it as:

def test_zip_float():
    return {"val1": 1.234, "val2": 3.456}

print(test_zip_float())

but it looks clumsy.

What is the best way to return multiple float values in a function?


Solution

  • You can do without zip as well. It will return a tuple of float values:

    def test_zip_float():
        return 1.234, 3.456
    
    print(test_zip_float())
    
    #(1.234, 3.456)