thank you for all your answers
-------- Original Message -------- From: Norman Khine <[email protected]> Apparently from: [email protected] To: [email protected] Subject: Re: [Tutor] dictionaries help Date: Thu, 23 Jul 2009 21:59:01 +0100 > 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<[email protected]> wrote: > > > > "Alan Gauld" <[email protected]> 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 - [email protected] > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - [email protected] > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
