python: ValueError: too many values to unpack (expected 2) data from excell

We Are Going To Discuss About python: ValueError: too many values to unpack (expected 2) data from excell. So lets Start this Python Article.

python: ValueError: too many values to unpack (expected 2) data from excell

  1. How to solve python: ValueError: too many values to unpack (expected 2) data from excell

    When you're getting an error from your code, it would help to pose the actual traceback, especially the part that indicates which line of your sample code is causing the error.
    When you call a function that returns multiple values, you can “unpack” it into individual variables. ValueError: too many values to unpack (expected 2) means that you called a function that only returns a single value, but you tried to unpack the return value into two variables.
    For example, consider this little python script:
    def returns_1_val(): return 'one' def returns_2_vals(): return 'one', 'two' print(returns_2_vals()) # Unpack the return value. x,y = returns_2_vals() print('x', x) print('y', y) print(returns_1_val()) # This next call fails. We're "expecting" Python to unpack 2 values into # x and y, but it fails because the function only returned one value. x,y = returns_1_val()
    When you run it:
    ('one', 'two') x one y two one Traceback (most recent call last): File "unpack_err.py", line 11, in <module> x,y = returns_1_val() ValueError: too many values to unpack (expected 2)
    The more general error message makes it a little more clear. For example, if you try to call x,y,z = return_2_vals(), you'll get
    ValueError: not enough values to unpack (expected 3, got 2)

  2. python: ValueError: too many values to unpack (expected 2) data from excell

    When you're getting an error from your code, it would help to pose the actual traceback, especially the part that indicates which line of your sample code is causing the error.
    When you call a function that returns multiple values, you can “unpack” it into individual variables. ValueError: too many values to unpack (expected 2) means that you called a function that only returns a single value, but you tried to unpack the return value into two variables.
    For example, consider this little python script:
    def returns_1_val(): return 'one' def returns_2_vals(): return 'one', 'two' print(returns_2_vals()) # Unpack the return value. x,y = returns_2_vals() print('x', x) print('y', y) print(returns_1_val()) # This next call fails. We're "expecting" Python to unpack 2 values into # x and y, but it fails because the function only returned one value. x,y = returns_1_val()
    When you run it:
    ('one', 'two') x one y two one Traceback (most recent call last): File "unpack_err.py", line 11, in <module> x,y = returns_1_val() ValueError: too many values to unpack (expected 2)
    The more general error message makes it a little more clear. For example, if you try to call x,y,z = return_2_vals(), you'll get
    ValueError: not enough values to unpack (expected 3, got 2)

Solution 1

When you’re getting an error from your code, it would help to pose the actual traceback, especially the part that indicates which line of your sample code is causing the error.

When you call a function that returns multiple values, you can “unpack” it into individual variables. ValueError: too many values to unpack (expected 2) means that you called a function that only returns a single value, but you tried to unpack the return value into two variables.

For example, consider this little python script:

def returns_1_val(): 
    return 'one'

def returns_2_vals(): 
    return 'one', 'two'

print(returns_2_vals())

# Unpack the return value.
x,y = returns_2_vals()
print('x', x)
print('y', y)

print(returns_1_val())

# This next call fails.  We're "expecting" Python to unpack 2 values into 
# x and y, but it fails because the function only returned one value.   
x,y = returns_1_val()

When you run it:

('one', 'two')
x one
y two
one
Traceback (most recent call last):
  File "unpack_err.py", line 11, in <module>
    x,y = returns_1_val()
ValueError: too many values to unpack (expected 2)

The more general error message makes it a little more clear. For example, if you try to call x,y,z = return_2_vals(), you’ll get

ValueError: not enough values to unpack (expected 3, got 2)

Original Author Tom Bryan Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment