[Tutor] Question on dictionary
Steven D'Aprano
steve at pearwood.info
Fri Sep 12 18:59:36 CEST 2014
On Fri, Sep 12, 2014 at 08:26:29PM +0530, Sunil Tech wrote:
> Hi All,
>
> i have a dictionary like
>
> res = [{'description': 'Testo', 'id': '676', 'parentOf': True},
> {'description': 'Pesto', 'id': '620', 'parentOf': False}]
That is not a dictionary. It is a list containing two dictionaries.
> i looking for the result like this
>
> res = [{'description': 'Testo', 'id': '676',
> 'id_desc':'676_Testo', 'parentOf': True},
> {'description': 'Pesto', 'id': '620',
> 'id_desc':'620_Pesto', 'parentOf': False}]
>
> to get this result i wrote a code like this
>
> for i in res:
> dict = {}
> dict['id_desc'] = str(i['id'])+','+str(i['description'])
> i.update(dict)
>
> is there any other simple methods to achieve this?
for d in res:
d['id_desc'] = d['id'] + '_' + d['description']
d['parentOf'] = (d['id'] == 676)
--
Steven
More information about the Tutor
mailing list