Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15313
| References | <16245908.783.1320346014867.JavaMail.geo-discussion-forums@yqhd1> <j8v1h5$4mc$1@dough.gmane.org> <4EB31D52.3080707@tim.thechases.com> |
|---|---|
| Date | 2011-11-04 10:59 +1100 |
| Subject | Re: Dictionary sorting |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2420.1320364788.27778.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web