Excellent responses so far. Dictionaries are optimized for retrieving key/value pairs. And to achieve that, it compromises on the order in which stuff is stored.
This and more is very nicely presented in the Pycon 2010 talk - The Mighty Dictionary. Highly recommended. http://us.pycon.org/2010/conference/schedule/event/12/ <http://us.pycon.org/2010/conference/schedule/event/12/>/jeff On Wed, Aug 18, 2010 at 11:04 PM, Vinay Shastry <vinayshas...@gmail.com>wrote: > On 18 August 2010 22:58, Anand Shankar <anand_shan...@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'] > <snip> > > I have no clues. Any inputs?? > > > > For this reason: > "It is best to think of a dictionary as an unordered set of key: value > pairs" > > http://docs.python.org/tutorial/datastructures.html#dictionaries > > >>> d = {'apple':2,'banana':5, 'coke': 6} > >>> d > {'coke': 6, 'apple': 2, 'banana': 5} > >>> print d.keys() > ['coke', 'apple', 'banana'] > > -- > Vinay S Shastry > http://thenub.one09.net > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers