-2

I want to create a dictionary which will have all characters in the alphabet as keys. I do that in the following way:

import string

origAlphabetUpCase = string.ascii_uppercase
print origAlphabetUpCase

upCaseDict = dict((el,'') for el in origAlphabetUpCase)

The result I get is as follows:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
{'A': '', 'C': '', 'B': '', 'E': '', 'D': '', 'G': '', 'F': '', 'I': '', 'H': '', 'K': '', 'J': '', 'M': '', 'L': '', 'O': '', 'N': '', 'Q': '', 'P': '', 'S': '', 'R': '', 'U': '', 'T': '', 'W': '', 'V': '', 'Y': '', 'X': '', 'Z': ''}

For some reason the order of keys is not as it's in the initial string. It seems like character pairs were swapped (except A and Z).

Any idea how and why that happens?

Eugene S
  • 6,709
  • 8
  • 57
  • 91
  • 3
    [see this post](http://stackoverflow.com/questions/5629023/key-order-in-python-dicionaries). `dict` is hashed, so basically no order. Or you could use [`OrderedDict`](http://docs.python.org/2/library/collections.html#collections.OrderedDict) – gongzhitaao Dec 02 '13 at 04:01
  • For the big picture: https://en.wikipedia.org/wiki/Hash_map – Felix Kling Dec 02 '13 at 04:05
  • Since you access dictionaries by their keys, the order in which the the dictionary is displayed is not important. – Burhan Khalid Dec 02 '13 at 08:18

3 Answers3

0

Python dictionaries don't have any order.

From the 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).

If you read further,

The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it).

So you can either use sorted function or OrderedDict.

0xc0de
  • 8,028
  • 5
  • 49
  • 75
0

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).

The main operations on a dictionary are storing a value with some key and extracting the value given the key, it really not matters which element is inserted at what time in the list it may appear in the middle of the list or starting or at the end too. Given example written in the docs.python.org says

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
Lalit Sachdeva
  • 6,469
  • 2
  • 19
  • 25
0

A dictionary is key is a hashable element. the ordering is not ordered due to the dictionary using a hash table for efficient look-up of the keys and the values of those keys.

A set in python is also unordered because the elements in the set are hashed, thus look-up is fast

if 'z' in ('x','y','z'):
  return 'z'

Would have a lookup of O(1) for instance as well vs O(n) if it were a list ['x','y','z'].

The following email from the python mailing list talks heavily about python dictionaries and the hashing that they do in technical detail.

https://mail.python.org/pipermail/python-list/2000-March/048085.html

klobucar
  • 6,307
  • 1
  • 13
  • 15