Dictionary Keys
I can not figure out the order of dictionary keys, they seemed not to be sorted. Any insight?
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
An example would be nice, one that surprises you by the outcome.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Here are a bunch of impel dictionaries that surprise me:
dic1 = {'a':1, 'b':2, 'c':3, 'd':4}
print dic1 # {'a': 1, 'c': 3, 'b': 2, 'd': 4}
dic2 = {1:'a', 2:"b", 3:'c', 4:'d'}
print dic2 # {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
dic3 = {11:'a', 22:"b", 3:'c', 4:'d'}
print dic3 # {3: 'c', 11: 'a', 4: 'd', 22: 'b'}
dic2 seems to behave as expected but dic1 and dic3 are surprise me.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
This little test using your example will show you that dictionary keys do have a certain order ...
# these all give the same result = {'a': 1, 'c': 3, 'b': 2, 'd': 4}
# so there must be a reason why keys are in a certain order!
dic11 = {'a':1, 'b':2, 'c':3, 'd':4}
print dic11
dic12 = {'a':1, 'd':4, 'b':2, 'c':3}
print dic12
dic13 = {'c':3, 'a':1, 'd':4, 'b':2}
print dic13
My reasoning would be that dictionary keys are ordered to make the lookup most efficient, a hash algorithm converting the key into an integer value would give such efficiency. I have not seen any official word on this.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
So there is some order, but we don't know what Python uses to create this order?
That is muchly confusing!
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
Look at it this way, a list assigns a numeric index to its elements. It starts at zero for the first element and then increases by one for each following element. To access the element you need to know this index number.
In the dictionary this index number has been replaced by a key, in many cases more meaningful then a number. So you don't really need an order, you need this key to access the associated value. The keys might have a perceived order, but it's not of any use.
If you want a quick sorted look of all your keys, go ahead and sort them like mawe has suggested.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
I think I get it! It is sinking in slowly into my yellow brains! So key is something more meaningfull then a just number.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
By gosh, you got it! You may call this dictionary comprehension!
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Now my question is what kind of things can be used as key?
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
I would use strings, even when you make something like a phone-number:name dictionary. You could use integers, but strings can be made more readable with the phone numbers. There are some scientific applications where an integer is better. Remember keys have to be unique!
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Since I can reverse dictionary, then anything that can be value can also be key?
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184