[New-bugs-announce] [issue30311] random.shuffle pointlessly shuffles dicts

Steven D'Aprano report at bugs.python.org
Mon May 8 21:34:22 EDT 2017

New submission from Steven D'Aprano:

According to the documentation, random.shuffle() should accept a sequence. But it also accepts dicts, in which case it does nothing, expensively:

py> x = dict.fromkeys(range(10**6))
py> random.shuffle(x)
py> str(x)[:55] + "...}"
'{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, ...}'

I'm not sure if it is better to explicitly test for and reject dicts and mappings, or just document that sorting dicts will apparently succeed while doing nothing.

Do we want to support sorting OrderedDicts? That at least makes sense, since they have an order, but again shuffle() seemingly works but actually does nothing.

I lean towards explicitly testing for dicts and raising.

messages: 293265
nosy: rhettinger, steven.daprano
priority: normal
severity: normal
status: open
title: random.shuffle pointlessly shuffles dicts
type: behavior
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list