[Tutor] Aschenputtel problem

Kent Johnson kent37 at tds.net
Thu Sep 15 19:33:41 CEST 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)

I don't think so. You can write it as two list comprehensions which is shorter but it iterates the original list twice:
list1 = [item for item in original_list if condition(item)]
list2 = [item for item in original_list if not condition(item)]

Kent



More information about the Tutor mailing list