[BangPypers] Dictionary : An elementary question

Dipo Elegbede delegbede at dudupay.com
Thu Aug 19 06:56:07 CEST 2010


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.shashwat at gmail.com> wrote:
> 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??
>>
>
> You will most likely want to read about how Hash-tables work.
>
>
>> anand
>>
>>
>>
>>
>> _______________________________________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>
>
>
> --
> ~l0nwlf
> _______________________________________________
> BangPypers mailing list
> BangPypers at 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


More information about the BangPypers mailing list