1

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?

wjandrea
  • 23,210
  • 7
  • 49
  • 68
jjcasmar
  • 987
  • 1
  • 11
  • 24

1 Answers1

5

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')
buran
  • 11,840
  • 7
  • 28
  • 49
  • Thanks, that worked fine. Thanks for the tip about the f-string. I didn't know it was possible to use them this way – jjcasmar Jan 16 '21 at 19:29