The key and value parts are separate expressions. Use the conditional expression in just the key part:
args = {'job[%s]' % k if k != 'id' else k: v for k, v in job.iteritems()}
The :
is not part of either expression, only of the dictionary comprehension syntax. You also need to loop over both keys and values; in Python 2, use job.iteritems()
, Python 3, job.items()
.
Demo:
>>> job = {'id':1234, 'age':17, 'name':'dev'}
>>> {'job[%s]' % k if k != 'id' else k: v for k, v in job.iteritems()}
{'id': 1234, 'job[age]': 17, 'job[name]': 'dev'}