Sorting by item_in_another_list

Fredrik Lundh fredrik at pythonware.com
Wed Oct 25 03:07:39 EDT 2006


Paul Rubin wrote:

>> for example:
>>
>> A = [0,1,2,3,4,5,6,7,8,9,10]
>> B = [2,3,7,8]
>>
>> desired_result = [2,3,7,8,0,1,4,5,6,9,10]
> 
> How about:
> 
>   desired_result = B + sorted(x for x in A if x not in B)

assuming that "keep the existing order" means what it says, you might as 
well replace "sorted" with a list comprehension.

</F>




More information about the Python-list mailing list