python - How to unpack a list of tuples with enumerate? -


this question has answer here:

i stumbled upon unpack issue can not explain.

this works:

tuples = [('jhon', 1), ('jane', 2)]  name, score in tuples:     ... 

this works

for id, entry in enumerate(tuples):     name, score = entry     ... 

but not work:

for id, name, score in enumerate(tuples):     ... 

throwing valueerror: need more 2 values unpack exeption.

enumerate creates tuples list value , corresponding index. in case:

list(enumerate(tuples)) 

gives:

[(0, ('jhon', 1)), (1, ('jane', 2))] 

to unpack can try this:

for index, (name, id) in enumerate(tuples):      pass 

here, python paring index , tuple object on right side results on left side, , assigning.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -