[Tutor] Aschenputtel problem

Alan G alan.gauld at freenet.co.uk
Fri Sep 16 00:30:39 CEST 2005

>> [condition(i) and list1.append(i) or list2.append(i) for i in 
>> original]
> Hmm, no, this will always evaluate list2.append(i) because 
> the value of list1.append(i) is None. You could use
> [ (condition(i) and list1 or list2).append(i) for i in original ]

Oops! Good point, which emphasises the point about shorter not 
necessarily being better!

My personal approach would be two separate comprehensions(*) which 
keeps the intent very clear and keeps it short at the expense 
of iterating the original list twice.

(*) Or you could use the filter funtion twice if you want to 
    make the intent even more explicit...

Alan G.

More information about the Tutor mailing list