Dictionary order?

Dan Stromberg drsalists at gmail.com
Mon Aug 1 19:42:58 EDT 2022


On Mon, Aug 1, 2022 at 3:25 PM <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:

> On 2022-08-02 at 07:50:52 +1000,
> Chris Angelico <rosuav at gmail.com> wrote:
>
> > On Tue, 2 Aug 2022 at 07:48, <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
> > >
> > > On 2022-08-01 at 13:41:11 -0700,
> > > Dan Stromberg <drsalists at gmail.com> wrote:
> > >
> > > > keys = [5, 10, 15, 14, 9, 4, 1, 2, 8, 6, 7, 12, 11]
> > > >
> > > > dict_ = {}
> > > > for key in keys:
> > > >     dict_[key] = 1
> > >
> > > $ python
> > > Python 3.10.5 (main, Jun  6 2022, 18:49:26) [GCC 12.1.0] on linux
> > > Type "help", "copyright", "credits" or "license" for more information.
> > > >>> [hash(x) for x in range(20)]
> > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
> > >
> > > Just sayin'.  :-)
> >
> > Yes, but I'm pretty sure that's been true for a LONG time. The hashes
> > for small integers have been themselves for as long as I can remember.
> > But the behaviour of the dictionary, when fed such keys, is what's
> > changed.
>
> I'm not disputing either of those facts.  I'm pointing out that the
> apparently arbitrary order of a mapping's keys becomes obvious when you
> look at the hashes of those keys.
>

It looks like the relationship no longer holds at around keys =
list(range(250, 260))

But i == hash(i) holds for the first million values at least.


More information about the Python-list mailing list