[Tutor] advice on idiom replacing if test requested

Brian van den Broek broek at cc.umanitoba.ca
Mon Dec 12 01:15:10 CET 2005


Hi all,

I have a case like this toy code:

import random
list1 = [1,2,3]
list2 = ['a', 'b', 'c']
item = random.choice(list1 +list2)
if item in list1:
     others = list2
else:
     others = list1


Another way occurred to me, but I wonder if I'm being too cute:

item = random.choice(list1 +list2)
others = [list1, list2][item in list1]

I believe we can rely on True and False being 1 and 0 until Python 
3.0. But, even assuming that's right, I wonder if it is obscure to others.

Thanks and best,

Brian vdB



More information about the Tutor mailing list