Dictionary Keys question

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jan 30 18:09:36 EST 2008


En Wed, 30 Jan 2008 20:47:36 -0200, FireNWater <khoard at gmail.com> escribió:

> I'm curious why the different outputs of this code.  If I make the
> dictionary with letters as the keys, they are not listed in the
> dictionary in alphabetical order, but if I use the integers then the
> keys are in numerical order.
>
> I know that the order of the keys is not important in a dictionary,
> but I was just curious about what causes the differences.  Thanks!!
>
> list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
> list2 = [1,2,3,4,5,6,7,8]
>
> Dictionary = dict(zip(list1, list2))
> print Dictionary
>
> Dictionary1 = dict(zip(list2, list1))
> print Dictionary1

Dictionaries use the hash value of the keys to distribute them in  
"buckets". Compare these:

for key in list1: print key, hash(key)
for key in list2: print key, hash(key)

-- 
Gabriel Genellina




More information about the Python-list mailing list