Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41400

Just curious - I thought dict keys would maintain sequence

From Frank Millman <frank@chagford.com>
Subject Just curious - I thought dict keys would maintain sequence
Date 2013-03-18 09:26 +0200
Newsgroups comp.lang.python
Message-ID <mailman.3413.1363591587.2939.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Just curious - I thought dict keys would maintain sequence Frank Millman <frank@chagford.com> - 2013-03-18 09:26 +0200

csiph-web