Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unpack enumerated zip in python

Tags:

python

I have three lists that I want to iterate on with an index. For that, I tried to use enumerate(zip(x, y, z)), but when I try to unpack that, it fails

[f.write('mVtet0.row({}) = Eigen::Vector3d({}, {}, {})\n'.format(i, x,y,z) for i, x, y, z in enumerate(zip(x,y,z))]

Gives the following error: ValueError: not enough values to unpack (expected 4, got 2)

I understand the issue, enumerate creates a tuple with the index and the result of the zip. What is the correct way to unpack everything?

like image 347
jjcasmar Avatar asked Oct 17 '25 13:10

jjcasmar


1 Answers

you get int and a tuple. Enclose x, y, z in brackets to make it tuple.

[f.write('mVtet0.row({}) = Eigen::Vector3d({}, {}, {})\n'.format(i, x,y,z) 
 for i, (x, y, z) in enumerate(zip(x,y,z))]

That said, in my opinion this is abuse of list comprehension with sole purpose to make it one-liner. Better use regular loop - it will be way more readable. And also better use f-strings.

for i, (x, y, z) in enumerate(zip(x,y,z)):
    f.write(f'mVtet0.row({i}) = Eigen::Vector3d({x}, {y}, {z})\n')
like image 175
buran Avatar answered Oct 21 '25 01:10

buran