Как создать словарь генератором?

Как написать следующую логику создания словаря генератором словаря.

left_children =  [1, 3, 5, 7, -1, 9, 11, -1, -1, -1, -1, -1, -1] # левые потомки
right_children = [2, 4, 6, 8, -1, 10, 12, -1, -1, -1, -1, -1, -1] # правые потомки
result = {}
for idx, (right, left) in enumerate(zip(left_children, right_children)):
    if right != -1:
        result[right] = idx
        result[left] = idx

Я попытался написать, что-то подобное, но это неверно:

{right:idx, left:idx for idx, (right, left) in 
                                           enumerate(zip(left_children, right_children)) 
                                                                        if right != -1 }

Как исправить?


Ответы (1 шт):

Автор решения: extrn
result = {
    k: idx
    for idx, (right, left) in enumerate(zip(left_children, right_children))
    if right != -1
    for k in (right, left)
}
→ Ссылка