473,383 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Is numeric keys of Python's dictionary automatically sorted?

I am coding a radix sort in python and I think that Python's dictionary may
be a choice for bucket.

The only problem is that dictionary is a mapping without order. But I just
found that if the keys are numeric, the keys themselves are ordered in the
dictionary.

part of my code is like this:
radix={}
for i in range(256):
radix[i]=[]

I checked and found that it is ordered like: {1:[], 2:[], 3[],...}

So I can just print out the contents of the dictionary in the desired order
without additional code.
I also tried adding new numeric keys and found that the dictionary's keys
are still ordered.

However, I am not sure whether it is always like this. Can anybody confirm
my finding?

Mar 7 '07 #1
11 11374
Ant
On Mar 7, 8:18 pm, "John" <rds1...@sh163.netwrote:
....
However, I am not sure whether it is always like this. Can anybody confirm
my finding?
>From the standard library docs:
"Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary's
history of insertions and deletions."

i.e. the behaviour you have discovered is an implementation detail,
and could change in future versions.

Mar 7 '07 #2
Then is there anyway to sort the numeric keys and avoid future implemetation
confusion?
"Ant" <an****@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
On Mar 7, 8:18 pm, "John" <rds1...@sh163.netwrote:
...
>However, I am not sure whether it is always like this. Can anybody
confirm
my finding?
>>From the standard library docs:

"Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary's
history of insertions and deletions."

i.e. the behaviour you have discovered is an implementation detail,
and could change in future versions.

Mar 7 '07 #3
On Wed, 2007-03-07 at 15:18 -0500, John wrote:
I am coding a radix sort in python and I think that Python's dictionary may
be a choice for bucket.

The only problem is that dictionary is a mapping without order. But I just
found that if the keys are numeric, the keys themselves are ordered in the
dictionary.
No.

The sequence of keys in a dictionary is a coincidental side effect of
the particular Python implementation, the number of keys, the values of
the keys, and the order in which the keys are inserted. You must not
rely on the keys appearing in any particular order.

Here is a simple counterexample that breaks the ordering, at least for
the version I'm running:
>>d = {}
for i in range(0,6): d[10**i] = []
....
>>d
{100000: [], 1: [], 100: [], 1000: [], 10: [], 10000: []}

-Carsten
Mar 7 '07 #4
John wrote:
Then is there anyway to sort the numeric keys and avoid future implemetation
confusion?
sorted(mydict.keys())

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Mar 7 '07 #5
On Mar 7, 3:49 pm, "John" <rds1...@sh163.netwrote:
Then is there anyway to sort the numeric keys and avoid future implemetation
confusion?

"Ant" <ant...@gmail.comwrote in message

news:11**********************@t69g2000cwt.googlegr oups.com...
On Mar 7, 8:18 pm, "John" <rds1...@sh163.netwrote:
...
However, I am not sure whether it is always like this. Can anybody
confirm
my finding?
>From the standard library docs:
"Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary's
history of insertions and deletions."
i.e. the behaviour you have discovered is an implementation detail,
and could change in future versions.
I would consider that a bad choice. In the dictionary the keys are a
set i.e. you might as well get a set() when you do d.keys() but the
set() came later so you just get a list. The problem with a list is
that somehow people want to make sense of it's order, just like in
this case. So if instead of asking, he could have just written the
application based on the fact that the keys will always be sorted in
some way. But then one day his application maybe run a different
platform and all of the sudden the keys are not sorted as before and
then disaster strikes.

I hope that dictionary.keys() would return a set to emphasize that
keys are unordered.

You suggested to just set a sort order and keep it consistent, but the
problem is that then you _have to_ maintain the sort order in addition
to the regular dictionary implementation (now it might just be a
byproduct), that is extra work that will have to be done on every
single insertion or deletion just for that rare use case where someone
will want the keys to be sorted.

Mar 8 '07 #6
On Wed, 07 Mar 2007 23:34:20 -0800, Nick Vatamaniuc wrote:
>>From the standard library docs:
"Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary's
history of insertions and deletions."
i.e. the behaviour you have discovered is an implementation detail,
and could change in future versions.

I would consider that a bad choice. In the dictionary the keys are a
set i.e. you might as well get a set() when you do d.keys() but the
set() came later so you just get a list.
The internal implementation of dictionaries are irrelevant.

Whether the keys "inside" a dictionary are "a set of keys" or "a list of
keys" or "a bunch of keys" is just semantics.

The problem with a list is
that somehow people want to make sense of it's order, just like in
this case.
And the problem with a dictionary is that some people want to make sense
of its order, just like in this case, and the fifty thousand previous
times people have asked this newsgroup how they can sort a dictionary.
So if instead of asking, he could have just written the
application based on the fact that the keys will always be sorted in
some way. But then one day his application maybe run a different
platform and all of the sudden the keys are not sorted as before and
then disaster strikes.
That would be his problem for being a bad coder, not our problem or the
language's fault.

There is no limit to the "could haves" that lead to disaster. Somebody
"could have" assumed that list.sort() returns the sorted list. Somebody
"could have" assumed that "is" is merely a synonym for "==".

And you know something? We get people making those mistakes all the time.
When people make those mistakes, it is *their responsibility*, for being
stupid or not reading the docs or not doing sufficient testing, or simply
for being inexperienced.

I hope that dictionary.keys() would return a set to emphasize that
keys are unordered.
What makes you think that people will magically intuit that sets are
unordered when they insist on thinking that dicts are ordered?

The below-average coder does this:
>>D = {1: None, 2: None, 3:None}
D
{1: None, 2: None, 3: None}

and concludes that dictionaries are ordered. If they are better-than
average, they might try this:
>>D = {1: None, 4: None, 3:None} # keys out of order
D
{1: None, 3: None, 4: None}

Still ordered, right? It's actually quite hard to get a dict with purely
integer keys out of order. So they ASS_U_ME that dicts are ordered.

What makes you think they won't do the same with sets?
>>set((1, 2, 3))
set([1, 2, 3])
>>set((1, 4, 3))
set([1, 3, 4])
>>D = {4: None, 2: None, 1: None, 3: None}
set(D.keys())
set([1, 2, 3, 4])

Sure looks ordered to me.

The solution is:

(1) Don't assume unordered data structures like sets and dicts are
ordered, even if they look like it. They aren't.

(2) If you want the keys of a dict sorted, get the keys, and sort them
when and as you need them.

(3) If you think you want a sorted dictionary, chances are you actually
want a different algorithm.

(4) But if you really do need a sorted dictionary, there are recipes on
the web. Google is your friend. But remember, they will be slower than
regular dictionaries.
--
Steven D'Aprano

Mar 8 '07 #7
Carsten Haese <ca*****@uniqsys.comwrote:
Here is a simple counterexample that breaks the ordering, at least for
the version I'm running:
>>>d = {}
for i in range(0,6): d[10**i] = []
...
>>>d
{100000: [], 1: [], 100: [], 1000: [], 10: [], 10000: []}
Here's another counterexample which shows that even dictionaries with
the same consecutively numbered small integer keys can vary the order in
which the keys are returned:
>>d1 = dict.fromkeys([1,2])
d2 = dict.fromkeys([9,1,2])
del d2[9]
d1
{1: None, 2: None}
>>d2
{2: None, 1: None}

In current C-Python implementations, the hash code for an integer is
simply the integer itself. That means there is a strong tendency for
consecutive integers to be stored in consecutive slots in the
dictionary. However as soon as you get gaps, or add the keys out of
order, there is a opportunity for higher valued keys to displace lower
valued keys into a different slot.

If you want the keys sorted then sort them.
Mar 8 '07 #8
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrote:
If they are better-than
average, they might try this:
>>>D = {1: None, 4: None, 3:None} # keys out of order
D
{1: None, 3: None, 4: None}

Still ordered, right? It's actually quite hard to get a dict with purely
integer keys out of order.
It isn't hard at all.

The next step would be to try something with a few more than 3 keys and
decide that you can't be bothered with all that typing and inventing
values.

dict.fromkeys([randint(0,99) for i in range(10)])

gives you keys out of order about 99.92% of the time.

Mar 8 '07 #9
"John" <rd*****@sh163.netwrites:
I am coding a radix sort in python and I think that Python's dictionary may
be a choice for bucket.
Why are you coding a radix sort?
The only problem is that dictionary is a mapping without order. But
I just found that if the keys are numeric, the keys themselves are
ordered in the dictionary.
Just an accident, dicts are implemented using hashing, and small
integers hash to themselves.
Mar 8 '07 #10
"John" <rd*****@sh163.netwrites:
I am coding a radix sort in python and I think that Python's dictionary may
be a choice for bucket.

The only problem is that dictionary is a mapping without order. But I just
found that if the keys are numeric, the keys themselves are ordered in the
dictionary.

part of my code is like this:
radix={}
for i in range(256):
radix[i]=[]
I wonder why nobody has suggested the use of a list:

redix = [[] for i in range(256)]

Matthias
Mar 8 '07 #11
Ant
On Mar 8, 8:20 am, Steven D'Aprano <s...@REMOVEME.cybersource.com.au>
wrote:
....
And the problem with a dictionary is that some people want to make sense
of its order, just like in this case, and the fifty thousand previous
times people have asked this newsgroup how they can sort a dictionary.
....
What makes you think they won't do the same with sets?
....
(1) Don't assume unordered data structures like sets and dicts are
ordered, even if they look like it. They aren't.

(2) If you want the keys of a dict sorted, get the keys, and sort them
when and as you need them.
All good points. Is this misconception really as common as you suggest
(obviously there aren't really going to be 50,000 previous threads of
this nature - but you know what I mean). Because if they are, it is
perhaps a case for adding optimized sorted_dict and sorted_set to the
collections module, similar to the TreeSet and TreeMap classes in
Java.

Mar 8 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Leif K-Brooks | last post by:
I need to associate a string (key) with an integer (value). A dictionary would do the job, except for the fact that it must be ordered. I also need to look up the value for a specific key easily,...
6
by: Noah | last post by:
I thought that Python has a builtin dictionary that associates global variable names with their values. I have forgotten how to do this and I can't seem to come up with the right search keywords to...
2
by: nickdevx | last post by:
Hi Say if I have a mixed array: $array = array("item1", "2"=>"item2", "5", "item4key"=>"item4") Is it possible while looping through the array (foreach ($array as $key=>$val)) to check if...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
1
by: tan | last post by:
Hi, I curently have 7 tables in my db, all with the same primary key, and 1 form showing all these fields (primary key form only 1 table). Is there anyway that once a primary key has been entered on...
4
by: aine_canby | last post by:
Hi, Do the Python Paths come in the form of a dictionary where I can access a particular path my its key in the registry? For example, in PythonWin Tools>>Edit Python Paths shows the name as...
2
by: james_027 | last post by:
hi, How could I transform something like this dict_1 = {'customer_id':1, 'item_id':3, amount:100} into dict_2 = {'customer':1, 'item':3, amount:100}
2
by: blaine | last post by:
Hey everyone, Just a friendly question about an efficient way to do this. I have a graph with nodes and edges (networkx is am amazing library, check it out!). I also have a lookup table with...
1
telgroc
by: telgroc | last post by:
I have a string stored in a text file. I need to read the string into python as a list of dictionaries. The format of data in the text file (called "input.txt") is as follows: <first name>,<last...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.