[Tutor] dictionaries help

davidwilson at Safe-mail.net davidwilson at Safe-mail.net
Fri Jul 24 00:14:01 CEST 2009


thank you for all your answers


-------- Original Message --------
From: Norman Khine <norman at khine.net>
Apparently from: tutor-bounces+davidwilson=safe-mail.net at python.org
To: tutor at python.org
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<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
> >
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list