'Need to generate a dictionary using comprehension

I have this code in python:

    layers = ['two', 'three', 'five', 'six']
    dict_toggle_bullet = {}
    for x, y in enumerate(layers):
       dict_toggle_bullet[y] = ["active" if j == y else "" for _, j in enumerate(layers)]

The output is:

{'two': ['active', '', '', ''], 'three': ['', 'active', '', ''], 'five': ['', '', 'active', ''], 'six': ['', '', '', 'active']}

Is there a way to convert this using dictionary comprehension in a single line?



Solution 1:[1]

This is what I came up with:

lst = ['two','three','five','six']
d = {lst[i]:['' if j != i else 'active' for j in range(4)] for i in range(4)}

However, I wouldn't advice you to do this as it seems a bit complicated and difficult to understand, so the direct method looks to be better.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prats