0

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?

1
  • Yepp the main reason being that if you assign X that way it won't be a list that you can iterate over the way you do because that will iterate over the 3 items one by one and not as a group. Try x = (('2015... ', ' 2015... ', 0.22),) and see the difference. Commented Mar 30, 2016 at 5:33

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

Comments

0

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] 

Comments