[Tutor] Manipulating Dictionary values

Sunil Tech sunil.techspk at gmail.com
Mon Dec 26 03:45:59 EST 2016


​​
Can this be achievable in one liner?

On Mon, Dec 26, 2016 at 1:33 PM, Sunil Tech <sunil.techspk at gmail.com> wrote:
>
> Hi Team,
>
> Dictionary is like
>
> a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'again'}, {'k':
'test', 'm': 'again'}]}
>
> I am trying to modify a value in the dictionary value at a['l'] & at 'm'
> expecting it to be
>
> a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'replaced'}, {'k':
'test', 'm': 'replaced'}]}
>
> for which I tried to do using list comprehension
>
> >>> a['l'] = [i['m'].replace('again', 'replaced') for i in a['l']]
> >>> a
> {'a': 'New', 'b': 'Two', 'l': ['replaced', 'replaced']}
>
> Any help will be appreciated.


More information about the Tutor mailing list