Here is a small script to sort the dictionary based on key/choice a = {"key3": 5 , "key2": 8, "key1": 2} b = {"key2": 7 , "key1": 4, "key3": 9} c = {"key1": 6 , "key3": 1, "key2": 1} undecorated = [a, b, c] # how do you sort this list?
sort_on = "key3" decorated = [(dict_[sort_on], dict_) for dict_ in [b]] decorated.sort() print decorated print "-----------------------" result = [dict_ for (key, dict_) in decorated] print result Praveen Kumar +91 9620621342 http://praveensunsetpoint.wordpress.com Bangalore On Thu, Aug 19, 2010 at 10:26 AM, Dipo Elegbede <delegb...@dudupay.com>wrote: > Hi All, > There really shouldn't be so much debate on the question asked. > Someone actually gave a direct and clear answer. I'm new at python and > his explanations were quite understandable. > As far as dictionaries are concerned, when you retrieve keys, there is > no guarantee of a particular order. > If you call the d.keys() consecutively, you might get two different orders. > However, to get it in an alphabetical order, there is a sort() method > to use. I am not too sure of the syntax but it should be something > like this: sort(d.keys()). > Please if I'm wrong point it out immediately to avoid transmitting wrong > codes. > I hope it helps. > Regards, > > On 8/19/10, Shashwat Anand <anand.shash...@gmail.com> wrote: > > On Wed, Aug 18, 2010 at 10:58 PM, 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'] > >> > >> 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?? > >> > > > > You will most likely want to read about how Hash-tables work. > > > > > >> anand > >> > >> > >> > >> > >> _______________________________________________ > >> BangPypers mailing list > >> BangPypers@python.org > >> http://mail.python.org/mailman/listinfo/bangpypers > >> > > > > > > > > -- > > ~l0nwlf > > _______________________________________________ > > BangPypers mailing list > > BangPypers@python.org > > http://mail.python.org/mailman/listinfo/bangpypers > > > > -- > Sent from my mobile device > > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise > Application Development > _______________________________________________ > 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