I have made a small demo of a more complex problem
def f(a):
return tuple([x for x in range(a)])
d = {}
[d['1'],d['2']] = f(2)
print d
# {'1': 0, '2': 1}
# Works
Now suppose the keys are programmatically generated
How do i achieve the same thing for this case?
n = 10
l = [x for x in range(n)]
[d[x] for x in l] = f(n)
print d
# SyntaxError: can't assign to list comprehension
{i+1:i for i in f(n)}[d[x] for x in l]would create a list of the elements of d, which are objects, not variables. Basically, eachd[x]doesn't resolve to "x'th position of d" but "object at x'th position of d". The outermost[]in[d['1'], d['2']]mean something different whether they are left or right elements of an assignment.