Better writing in python

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Wed Oct 24 10:04:28 EDT 2007


> On 2007-10-24, Alexandre Badez <alexandre.badez 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




More information about the Python-list mailing list