I'm using python 2.7.5 on OS X Mavericks and I'm seeing unexpected behavior with a dictionary I'm using to generate a simple text menu. My question is: are the integer keys in a Python dictionary sorted and sorted with priority? I can see that the mainMenu_1
dictionary (containing some numeric keys and some string keys) sorts the integer keys and then presents the string keys in the expected random order. mainMenu_2
is randomized as expected.
from the python 2.7.8 docs:
"It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary)."
mainMenu_1 = {
0: 'README',
1: 'New Set',
2: 'View Sets',
3: 'Quiz',
4: 'Scores',
5: 'Configuration Settings',
'Q': 'Quit',
'a': 'additional letter to test',
'b': 'additional letter to test'
}
mainMenu_2 = {
'one': 'README',
'two': 'New Set',
'three': 'View Sets',
'four': 'Quiz',
'five': 'Scores',
'six': 'Configuration Settings',
'Q': 'Quit',
'd': 'another letter to test'
}
print mainMenu_1.keys()
[0, 1, 2, 3, 4, 5, 'a', 'Q', 'b']
print mainMenu_2.keys()
['four', 'Q', 'five', 'three', 'd', 'six', 'two', 'one']
And a third test:
c = {1:'one','two':'two',3:'three'}
print c
{1: 'one', 3: 'three', 'two': 'two'}