[BangPypers] List compression imrpovement

Noufal Ibrahim noufal at nibrahim.net.in
Wed Jan 9 09:16:02 CET 2013


vijay <vnbang2003 at yahoo.com> writes:

> Hi,
>    I want to know if there is any other better way for improving below
> list compression. I have to use list compression only to achieve it.
>
>    say i have list   a=[1, 4, 9, 12, 15, 21] expected result (4, 12),
> (9, 12, 15, 21)

It's not clear how the first becomes the second. List comprehensions
transform one list into another. Your input is a list and output is two
tuples. 

>
>  few solutions are
>   1) [filter(None, l) for l in zip(*[(x if x % 2 == 0 else None, x if x % 3 == 0 else None) for x in a])]
>   2) [y for y in a if bool(y%2==0)],[y for y in a if bool(y%3==0)]

Unless this a throwaway code golf problem, you should ask yourself if
someone else reading this a month or two can understand what you were
intending here. If the answer is no, then rewrite this is a more verbose
but simpler way. 

[...]


-- 
Cordially,
Noufal
http://nibrahim.net.in


More information about the BangPypers mailing list