user5870009
user5870009

Reputation:

ValueError: too many values to unpack, can't figure out why

I have a function:

def store(word, info_list):
    for a, b, c, in info_list:
        data = {}
        ...

and I am calling:

store(x[0],x[1])

Where

x = (u'sergeev', (u'2015 afc asian cup group b', 
(u'2015 afc asian cup group b', u'sergeev', 372.57022256331544), 0.22388357256778307))

My goal is to make:

a=u'2015 afc asian cup group b'
b=(u'2015 afc asian cup group b', u'sergeev', 372.57022256331544)
c=0.22388357256778307

But I got

in store
for a,b,c, in info_list:
ValueError: too many values to unpack

I couldn't find where the mismatch was...can anyone help me out?

Upvotes: 0

Views: 63

Answers (2)

JRodDynamite
JRodDynamite

Reputation: 12613

Instead of using a for loop, simply unpack the elements.

def store(word, info_list):
    a, b, c = info_list

x[1] (the value you are passing to the function) is basically a simple tuple. Simply unpacking the values would suffice here.

You can use for loop when you have a tuple of tuples. Have a look at the example below:

>>> a = ((1, 2), (2, 3), (3, 4))
>>> for i, j in a:
...     print i, j  
1 2
2 3
3 4

Upvotes: 1

SDBot
SDBot

Reputation: 834

I don't that need to be looped, why not just assign it like this :

def store(word, info_list):
    a, b, c = info_list[0], info_list[1], info_list[2]

Upvotes: 0

Related Questions