Splitting lists

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Feb 27 06:05:32 EST 2003


Alex Martelli <aleax at aleax.it> wrote in 
news:2Vl7a.329137$AA2.12321414 at news2.tin.it:

> Yes, and I think it's the most Pythonic approach.  However, it
> would not impress people -- it's too clear and understandable,
> so everybody can see at once what it does, and nobody will be
> oohing and aahing about how "kewl" Python is.
> 
> More impressive...:
> 
> appenders = tl.append, fl.append
> for e in lst: appenders[not fn(e)](e)
> 
> now isn't THAT more like it...?
> 
Kewl, but still maintaing a semblance of readability might be:

>>> test = range(10)
>>> def odd(x): return x%2 != 0

>>> fl = []
>>> tl = [ x for x in test if odd(x) or fl.append(x) ]
>>> tl
[1, 3, 5, 7, 9]
>>> fl
[0, 2, 4, 6, 8]
>>> 



-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list