Just curious - I thought dict keys would maintain sequence

F

Frank Millman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top