[Tutor] Weird iteration using zip()

Alan Gauld alan.gauld at freenet.co.uk
Fri Oct 28 00:17:58 CEST 2005


>>>> a = ['a','b','c','d','e']
>>>> b = [1,2,3,4,5]
>>>> abdict = dict(zip(a,b))
>>>> abdict
> {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
> 
> 
> Why is this weird iteration going on"

Dictionaries store their values in the most efficient way for key lookup 
they do not store them in order. In fact the order can even change 
over the life of the dictionary. Think of a dictionary being like a tree 
rather than a list.

This is also why accessing data from a dictionary is often faster 
than from a list.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list