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
Post a Comment