
On Mon, Aug 03, 2020 at 03:04:40AM -0000, raymond.hettinger@gmail.com wrote:
Steven D'Aprano wrote:
This is easily solved with a three-line helper: def shuffled(iterable): ... I have implemented this probably a half a dozen times, and I expect others have too.
FWIW, we've already documented a clean way to do it, https://docs.python.org/3/library/random.html#random.shuffle , "To shuffle an immutable sequence and return a new shuffled list, use sample(x, k=len(x)) instead."
Yes, I remember the last time I played poker with some friends, and the dealer handed me the deck of cards and asked me to take a sample of 52 cards *wink* While you are technically correct that a sample of N from a sequence of length N is equivalent to shuffling, that's not a particularly obvious thing to do, and the semantics of shuffling and sampling are not the same. Hence the need to document it. According to my testing in Python 3.8, the version with sample is about 10% slower than the "shuffled" helper I gave. That wouldn't be too bad if the operation was fast, but for a sequence of 30,000 items on my computer, that takes nearly half a second. So a 10% slowdown is quite significant. I think I'll continue using my shuffled helper function, and while I personally won't re-raise this issue, I'll continue to give it my support next time somebody raises it. -- Steven