-2

I try to create this dic :

a = [1,2,3]
b = [4,5,6]
[{"numberfirst": x, "numbersecond":y} for (x,y) in (a,b)]

My goal is to get that :

[
 {"numberfirst": 1, "numbersecond":4},
 {"numberfirst": 2, "numbersecond":5},
 {"numberfirst": 3, "numbersecond":6},
]

But I got that :

ValueError: too many values to unpack (expected 2)

Anyone who can help me ?

Thank you very much !

Mike
  • 63
  • 2
  • 10
  • Does this answer your question? [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – buran Jan 29 '22 at 14:47
  • And to be precise you are doing list comprehension, not dict comprehension. – buran Jan 29 '22 at 14:48

1 Answers1

1

Use zip(a,b) instead (a,b)

l = [{"numberfirst": x, "numbersecond":y} for (x,y) in zip(a,b)]
Алексей Р
  • 7,507
  • 2
  • 7
  • 18