On Jan 5, 4:14 pm, [EMAIL PROTECTED] wrote: > On Jan 5, 5:07 pm, [EMAIL PROTECTED] wrote: > > > Hello, Paul and Arnaud. > > While I think about your answers: do you think there is any way to > > avoid shuffle? > > It may take unnecessary long on a long list most of whose elements > > have the property.
You could generate a random shuffle of range(len(seq)) lazily, and use that to iterate over your sequence. import random import itertools def randxrange(n): "Generate the numbers 0 to n-1 in a random order" x = range(n) for i in xrange(n): r = random.randrange(n - i) yield x[r] x[r] = x[n - i - 1] def shuffled(seq): "Generate the elements of seq in a random order" return (seq[i] for i in randxrange(len(seq))) def pick_random(seq, prop): return itertools.ifilter(prop, shuffled(seq)).next() -- Paul Hankin -- http://mail.python.org/mailman/listinfo/python-list