You are trying to do this:
{c: [x[0] for x in g16.nodes(data=True) if x[1]['color'] == c] for c in colors}
But it's not very efficient as you are looping over g16.nodes(data=True)
once for each color
Something like this is better
d = {c: [] for c in colors}
for x in g16.nodes(data=True):
k = x[1]['color']
if k in d:
d[k].append(x[0])
If you know k
is always in colors
, you could simplify to
d = {c: [] for c in colors}
for x in g16.nodes(data=True):
d[x[1]['color']].append(x[0])