
Aug. 1, 2020
10:03 p.m.
On Sat, Aug 01, 2020 at 08:54:16PM +0300, Ram Rachum wrote:
When writing some code now, I needed to produce a shuffled version of `range(10, 10 ** 5)`.
This is one way to do it:
shuffled_numbers = list(range(10, 10 ** 5)) random.shuffle(shuffled_numbers)
I don't like it because (1) it's too imperative and (2) I'm calling the list "shuffled" even before it's shuffled.
This is easily solved with a three-line helper: def shuffled(iterable): L = list(iterable) random.shuffle(L) return L I have implemented this probably a half a dozen times, and I expect others have too. I agree with Alex that this would make a nice addition to the random module. -- Steven