[BangPypers] Dictionary : An elementary question

Vivek Khurana hiddenharmony at gmail.com
Wed Aug 18 19:34:25 CEST 2010


On Wed, Aug 18, 2010 at 10:58 PM, Anand Shankar <anand_shankar at yahoo.com> wrote:
> During a tutorial python session with my colleagues I was presented with a basic
> question
>
>>>> d = {'apple':2,'banana':5, 'coke': 6}
>>>> print d.keys()
> ['coke', 'apple', 'banana']
>
>
> Question is why does it not return
>
> ['apple','banana','coke']
>
> Similarly:
>
>>>> d = {'a':2,'b':4,'c':5,'d':4,'e':3}
>>>> print d.keys()
> ['a', 'c', 'b', 'e', 'd']
>
> why not
>
> ['a', 'b', 'c', 'd', 'e']
>
> I have no clues. Any inputs??

 Quoting from python docs[1]

  "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 sort() method to the list of keys). "

[1]: http://docs.python.org/tutorial/datastructures.html#dictionaries

regards
Vivek
-- 
The hidden harmony is better than the obvious!!


More information about the BangPypers mailing list