Multiple iterators in "for", "lfor", etc. with something like "dct.items()"

Viewed 46

How could I convert the following to hy?

dct = { 1:2, 3:4 }
print([i*j for i, j in dct.items()])
# => [2, 12]
1 Answers

I'm puzzled by your title, since there's only one iteration clause in your example, but anyway, here's a literal Hy translation:

(setv dct {1 2  3 4})
(print (lfor  [i j] (.items dct)  (* i j)))
; => [2, 12]

But you could also do this with the shadow multiplication operator:

(import  hy.pyops *)
(setv dct {1 2  3 4})
(print #* (map * (.keys dct) (.values dct)))
; => 2 12
Related