[Tutor] Aschenputtel problem

Bob Gailer bgailer at gmail.com
Fri Sep 16 03:39:57 CEST 2005


At 10:12 AM 9/15/2005, Christopher Arndt wrote:

Hi,

I wonder if there is a shorter form of the following idiom:

list1 = []
list2 = []
for item in original_list:
if condition(item):
list1.append(item)
else:
list2.append(item)


Consider (5 lines instead of 7):

lists = [[], []]
for item in original_list: 
lists[condition(item)].append(item)
list1 = lists[0]
list2 = lists[1]

This assumes condition() returns 0 or 1 (True)

or if you don't mind the result in sets (assumes unique elements):

set1 = set([x for x in original_list if cond(x)])
set2 = original_list - set1 

I can't send mail my usual way due to unknown problems. So am using gmail. 
Please relpy as always to bgailer at alum.rpi.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050915/cbc90026/attachment.html


More information about the Tutor mailing list