2

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
  • 28,235
  • 9
  • 60
  • 81
jjcasmar
  • 1,387
  • 1
  • 18
  • 30

1 Answers1

6

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
  • 13,682
  • 10
  • 36
  • 61
  • 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