[Tutor] dictionaries help

Norman Khine norman at khine.net
Thu Jul 23 22:59:01 CEST 2009


Also you can use list comprehension

In [1]: my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb',
'name': 'b name'}]

In [2]: my_code = 'aaa'

In [3]: print [d for d in my_lst if d['code'] == my_code]
------> print([d for d in my_lst if d['code'] == my_code])
[{'code': 'aaa', 'name': 'a name'}]

In [4]:


On Thu, Jul 23, 2009 at 6:04 PM, Alan Gauld<alan.gauld at btinternet.com> wrote:
>
> "Alan Gauld" <alan.gauld at btinternet.com> wrote
> Oops!
> Should be:
>
>> def findDict(value, dictList):
>>  for dct in dictList:
>>      if dct.get('code', '') == my_code
>
>        if dct.get('code', '') == value
>
>>         return dct
>
>
>> HTH,
>>
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list