Is there a pythonic way to use items in a list to define a dictionary's key and values?
For example, I can do it with:
s = {} a = ['aa', 'bb', 'cc'] for a in aa: s['text_%s' %a] = 'stuff %s' %a In [27]: s Out[27]: {'text_aa': 'stuff aa', 'text_bb': 'stuff bb', 'text_cc': 'stuff cc'} I wanted to know if its possible to iterate over the list with list comprehension or some other trick.
something like:
s[('text_' + a)] = ('stuff_' + a) for a in aa thanks!
{f'text_{a}': f'stuff_{a}' for a in aa}(from Python 3.6).dict(zip(("text_" + x for x in a), ("stuff " + x for x in a)))but dict comprehension, as mentioned in the answers, is a better approach.