I am new to python. I\'m trying to write this
if x not in d: d[x] = {} q = d[x]
in a more compact way using the ternary operator
In Python, assignments cannot occur in expressions, therefore you can't write code like a = (b = c).
a = (b = c)
You're looking for setdefault:
q = d.setdefault(x, {})
Alternative, use a defaultdict.