Better writing in python

beginner zyzhu2000 at gmail.com
Wed Oct 24 12:30:30 EDT 2007


On Oct 24, 9:04 am, "A.T.Hofkamp" <h... at se-162.se.wtb.tue.nl> wrote:
> > On 2007-10-24, Alexandre Badez <alexandre.ba... at gmail.com> wrote:
> > I'm just wondering, if I could write a in a "better" way this
> > code
>
> > lMandatory = []
> > lOptional = []
> > for arg in cls.dArguments:
> >   if arg is True:
> >     lMandatory.append(arg)
> >   else:
> >     lOptional.append(arg)
> > return (lMandatory, lOptional)
>
> > I think there is a better way, but I can't see how...
>
> You can do it shorter, not sure that it also qualifies as better....
>
> d = { True : [] , False : [] }
> for arg in cls.arguments:
>   d[arg == True].append(arg)
>
> return d[True], d[False]
>
> One potential problem here is that 'arg == True' may not be the same as 'if
> arg:'. I couldn't come up with a better equivalent expression, maybe one of the
> other readers knows more about this?
>
> Albert

d[bool(arg)].append(arg) resolves your concern?




More information about the Python-list mailing list