Just curious - I thought dict keys would maintain sequence
Frank Millman
frank at chagford.com
Mon Mar 18 03:26:19 EDT 2013
Hi all
I know that you cannot rely on the order of keys in a dictionary, and I
am not attempting to do so.
Nevertheless, the following surprised me. A program creates a dictionary
with a known set of keys. I would have thought that multiple runs of the
program would return the keys in the same sequence. As you can see, the
results appear to be totally random.
Just out of interest, I would appreciate an explanation.
Thanks
Frank Millman
F:\>type test.py
test_list = [
'ert', 'jhg', 'yer', 'nli', 'vrg',
'qnh', 'kyh', 'cgj', 'lys', 'vyk',
'dws', 'fuj', 'ghj', 'puy', 'brc',
'dhy', 'jkl', 'gfd', 'jtf', 'dfp']
test_dict = dict([(x, None) for x in test_list])
print(test_dict.keys())
F:\>test.py
dict_keys(['ghj', 'qnh', 'lys', 'ert', 'jhg', 'dfp', 'vrg', 'dws',
'jtf', 'puy',
'yer', 'brc', 'kyh', 'jkl', 'gfd', 'nli', 'fuj', 'dhy', 'cgj', 'vyk'])
F:\>test.py
dict_keys(['vrg', 'dfp', 'brc', 'yer', 'nli', 'jhg', 'cgj', 'ert',
'dws', 'gfd',
'puy', 'vyk', 'ghj', 'jkl', 'jtf', 'kyh', 'qnh', 'fuj', 'dhy', 'lys'])
F:\>test.py
dict_keys(['yer', 'nli', 'brc', 'fuj', 'cgj', 'dfp', 'dhy', 'vrg',
'jkl', 'jtf',
'vyk', 'ghj', 'qnh', 'kyh', 'gfd', 'ert', 'dws', 'jhg', 'lys', 'puy'])
Python version is 3.3.0, win32.
More information about the Python-list
mailing list