Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15300 > unrolled thread

Dictionary sorting

Started byScott Ware <scottdware@gmail.com>
First post2011-11-03 11:46 -0700
Last post2011-11-04 10:59 +1100
Articles 11 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  Dictionary sorting Scott Ware <scottdware@gmail.com> - 2011-11-03 11:46 -0700
    Re: Dictionary sorting John Gordon <gordon@panix.com> - 2011-11-03 18:57 +0000
      Re: Dictionary sorting Scott Ware <scottdware@gmail.com> - 2011-11-03 12:00 -0700
        Re: Dictionary sorting Chris Kaynor <ckaynor@zindagigames.com> - 2011-11-03 12:13 -0700
    Re: Dictionary sorting insomnia@gmail.com - 2011-11-03 13:01 -0700
    Re: Dictionary sorting Terry Reedy <tjreedy@udel.edu> - 2011-11-03 17:36 -0400
    Re: Dictionary sorting Tim Chase <python.list@tim.thechases.com> - 2011-11-03 18:01 -0500
      Re: Dictionary sorting Ben Finney <ben+python@benfinney.id.au> - 2011-11-04 11:06 +1100
        Re: Dictionary sorting Hrvoje Niksic <hniksic@xemacs.org> - 2011-11-04 11:20 +0100
          Re: Dictionary sorting Ben Finney <ben+python@benfinney.id.au> - 2011-11-04 23:19 +1100
    Re: Dictionary sorting Chris Angelico <rosuav@gmail.com> - 2011-11-04 10:59 +1100

#15300 — Dictionary sorting

FromScott Ware <scottdware@gmail.com>
Date2011-11-03 11:46 -0700
SubjectDictionary sorting
Message-ID<16245908.783.1320346014867.JavaMail.geo-discussion-forums@yqhd1>
Python newbie here. So, when creating dictionaries, I am noticing that each time I print it out, that its not in the same order as when I typed it in. They seem to be getting sorted somehow. Is there a way to not sort them and leave the order as is?

Thanks!

[toc] | [next] | [standalone]


#15301

FromJohn Gordon <gordon@panix.com>
Date2011-11-03 18:57 +0000
Message-ID<j8uo6n$gu6$1@reader1.panix.com>
In reply to#15300
In <16245908.783.1320346014867.JavaMail.geo-discussion-forums@yqhd1> Scott Ware <scottdware@gmail.com> writes:

> Python newbie here. So, when creating dictionaries, I am noticing that
> each time I print it out, that its not in the same order as when I typed
> it in. They seem to be getting sorted somehow. Is there a way to not sort
> them and leave the order as is?

Dictionaries don't maintain the order of the items.

If you want to keep track of the order in which the items were inserted,
you'll need to do that yourself using a separate mechanism (a list, for
example.)

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#15302

FromScott Ware <scottdware@gmail.com>
Date2011-11-03 12:00 -0700
Message-ID<9836933.743.1320346827670.JavaMail.geo-discussion-forums@yqbl36>
In reply to#15301
Great! Thanks, John for the quick response!

[toc] | [prev] | [next] | [standalone]


#15303

FromChris Kaynor <ckaynor@zindagigames.com>
Date2011-11-03 12:13 -0700
Message-ID<mailman.2411.1320347649.27778.python-list@python.org>
In reply to#15302
Note that there are a number of recipes available for free online, and
if you are using a newer version of Python (2.7 or higher), the
collections module includes an OrderedDict class
(http://docs.python.org/library/collections.html#collections.OrderedDict
- this also include a library for Python 2.4 and higher as well). Note
that there are a lot of different possible behaviors, so any
particular recipe may or may not behave exactly as you desire.

Chris



On Thu, Nov 3, 2011 at 12:00 PM, Scott Ware <scottdware@gmail.com> wrote:
> Great! Thanks, John for the quick response!
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#15304

Frominsomnia@gmail.com
Date2011-11-03 13:01 -0700
Message-ID<19322439.785.1320350496479.JavaMail.geo-discussion-forums@yqpp12>
In reply to#15300
Moreover, for on-the-fly ordering you can consider to use sorted() on yourdict.keys(), like:

for k in sorted(yourdict.keys()):
    print k, yourdict[k]

I think it has no side effects, except that the orderering can be slow on huge data sets, and that you need to call it every time after updating the dict keys.

Regards,
insomniac

[toc] | [prev] | [next] | [standalone]


#15307

FromTerry Reedy <tjreedy@udel.edu>
Date2011-11-03 17:36 -0400
Message-ID<mailman.2414.1320356213.27778.python-list@python.org>
In reply to#15300
On 11/3/2011 2:46 PM, Scott Ware wrote:
> Python newbie here. So, when creating dictionaries, I am noticing
> that each time I print it out, that its not in the same order as when
> I typed it in. They seem to be getting sorted somehow.

No, the entries are not being sorted at all.

 > Is there a way to not sort them and leave the order as is?

CPython iterates (and prints) dict items in their arbitrary internal 
hash table order, which depends on the number and entry order of the 
items. It is a bug to depend on that arbitrary order in any way.

If you want to keep the dict sorted by entry order (as far as the user 
view goes), use collections.OrderedDict.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#15311

FromTim Chase <python.list@tim.thechases.com>
Date2011-11-03 18:01 -0500
Message-ID<mailman.2418.1320361310.27778.python-list@python.org>
In reply to#15300
On 11/03/11 16:36, Terry Reedy wrote:
>   >  Is there a way to not sort them and leave the order as is?
>
> CPython iterates (and prints) dict items in their arbitrary internal
> hash table order, which depends on the number and entry order of the
> items. It is a bug to depend on that arbitrary order in any way.

Does this "never trust it" hold even for two consecutive 
iterations over an unchanged dict?  I didn't see anything in the 
docs[1] to make such a claim, but at least from my experience, 
one can reliably assert

   d = {}
   randomly_populate(d)
   list1 = list(d.iterkeys())
   list2 = list(d.iterkeys())
   assert list1 == list2

I understand all bets are off if one adds/removes (or maybe 
changes the values) of the dict between iterations, but I didn't 
know if what I saw was merely an implementation detail that 
shouldn't be counted on.

-tkc

[1]
http://docs.python.org/library/stdtypes.html#mapping-types-dict


[toc] | [prev] | [next] | [standalone]


#15314

FromBen Finney <ben+python@benfinney.id.au>
Date2011-11-04 11:06 +1100
Message-ID<8739e4n43n.fsf@benfinney.id.au>
In reply to#15311
Tim Chase <python.list@tim.thechases.com> writes:

> On 11/03/11 16:36, Terry Reedy wrote:
> > CPython iterates (and prints) dict items in their arbitrary internal
> > hash table order, which depends on the number and entry order of the
> > items. It is a bug to depend on that arbitrary order in any way.
>
> Does this "never trust it" hold even for two consecutive iterations
> over an unchanged dict? I didn't see anything in the docs[1] to make
> such a claim,

Exactly. The order of retrieval is entirely up to the implementation.
There is no guarantee that the order will be the same the next time you
iterate over the same dict. It is a bug to depend on a reliable order of
retrieving the items.

> I didn't know if what I saw was merely an implementation detail that
> shouldn't be counted on.

If the docs don't specify some particular observed behaviour in Python,
you should consider it an implementation detail.

-- 
 \       “Pray, v. To ask that the laws of the universe be annulled in |
  `\     behalf of a single petitioner confessedly unworthy.” —Ambrose |
_o__)                           Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#15332

FromHrvoje Niksic <hniksic@xemacs.org>
Date2011-11-04 11:20 +0100
Message-ID<87d3d8mbop.fsf@xemacs.org>
In reply to#15314
Ben Finney <ben+python@benfinney.id.au> writes:

> Tim Chase <python.list@tim.thechases.com> writes:
>
>> On 11/03/11 16:36, Terry Reedy wrote:
>> > CPython iterates (and prints) dict items in their arbitrary internal
>> > hash table order, which depends on the number and entry order of the
>> > items. It is a bug to depend on that arbitrary order in any way.
>>
>> Does this "never trust it" hold even for two consecutive iterations
>> over an unchanged dict? I didn't see anything in the docs[1] to make
>> such a claim,
>
> Exactly.

This is false.  The docs say:

    If items(), keys(), values(), iteritems(), iterkeys(), and
    itervalues() are called with no intervening modifications to the
    dictionary, the lists will directly correspond. This allows the
    creation of (value, key) pairs using zip(): pairs = zip(d.values(),
    d.keys()).

(http://docs.python.org/library/stdtypes.html#mapping-types-dict)

> The order of retrieval is entirely up to the implementation.

This part is still true, but the order won't change behind your back if
you're not touching the dict.

[toc] | [prev] | [next] | [standalone]


#15338

FromBen Finney <ben+python@benfinney.id.au>
Date2011-11-04 23:19 +1100
Message-ID<87y5vwkrle.fsf@benfinney.id.au>
In reply to#15332
Hrvoje Niksic <hniksic@xemacs.org> writes:

> Ben Finney <ben+python@benfinney.id.au> writes:
>
> > Tim Chase <python.list@tim.thechases.com> writes:
> >> Does this "never trust it" hold even for two consecutive iterations
> >> over an unchanged dict? I didn't see anything in the docs[1] to make
> >> such a claim,
> >
> > Exactly.
>
> This is false.  The docs say:
>
>     If items(), keys(), values(), iteritems(), iterkeys(), and
>     itervalues() are called with no intervening modifications to the
>     dictionary, the lists will directly correspond. This allows the
>     creation of (value, key) pairs using zip(): pairs = zip(d.values(),
>     d.keys()).
>
> (http://docs.python.org/library/stdtypes.html#mapping-types-dict)

Thank you for this correction.

-- 
 \       “Firmness in decision is often merely a form of stupidity. It |
  `\        indicates an inability to think the same thing out twice.” |
_o__)                                                —Henry L. Mencken |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#15313

FromChris Angelico <rosuav@gmail.com>
Date2011-11-04 10:59 +1100
Message-ID<mailman.2420.1320364788.27778.python-list@python.org>
In reply to#15300
On Fri, Nov 4, 2011 at 10:01 AM, Tim Chase
<python.list@tim.thechases.com> wrote:
>  list1 = list(d.iterkeys())
>  list2 = list(d.iterkeys())
>  assert list1 == list2
>

There is such a guarantee in Python 2. From
http://docs.python.org/library/stdtypes.html:
"If items(), keys(), values(), iteritems(), iterkeys(), and
itervalues() are called with no intervening modifications to the
dictionary, the lists will directly correspond. This allows the
creation of (value, key) pairs using zip(): pairs = zip(d.values(),
d.keys()). The same relationship holds for the iterkeys() and
itervalues() methods: pairs = zip(d.itervalues(), d.iterkeys())
provides the same value for pairs. Another way to create the same list
is pairs = [(v, k) for (k, v) in d.iteritems()]."

Python 3 does things quite differently (with views), and I can't find
a corresponding promise, but I expect that this would still be the
case.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web