Misuse of list comprehensions?

John Salerno johnjsal at NOSPAMgmail.com
Tue May 20 13:25:07 EDT 2008


"Bruno Desthuilliers" <bruno.42.desthuilliers at websiteburo.invalid> wrote in 
message news:4832dbd0$0$23017$426a74cc at news.free.fr...

>> def compress(s):
>>     new = []
>>     [new.append(c) for c in s if c not in new]
>>     return ''.join(new)
>
> As far as I'm concerned (and I'm a big fan of list-comps, generator 
> expressions etc), this is definitively an abuse. And a waste of resources 
> since it builds  a useless list of 'None'.

I agree with you. After being proud of myself for about 20 seconds, it 
suddenly seemed like an abuse rather than a clever way to do it in one line. 
:) It just looked wrong to have a list comp. without an assignment, etc.

Actually, though, I wasn't so much thinking of the waste of resources as I 
was that what was happening didn't seem to be explicit or evident enough. So 
despite the fact that the for loop with the nested if is the exact same 
code, it just seems more appropriate. 





More information about the Python-list mailing list