[Tutor] minor display issue with python dictionaries

Joel Goldstick joel.goldstick at gmail.com
Sun Nov 24 17:55:29 CET 2013


On Sun, Nov 24, 2013 at 11:03 AM, Reuben <reuben.dlink at gmail.com> wrote:
> Hi,
>
> ##########################################
>>>>
>>>> new_dict = {'a':10, 'b' :20, 'c': 30,'d' : 40}
>>>>
>>>>
>>>> print new_dict
> {'a': 10, 'c': 30, 'b': 20, 'd': 40}
>>>>
>
>
> #########################################
>
>
> From the above output, I see key 'c' is at third position during input, but
> while displaying the output it is displayed at second position
>
> Although, I dont see any impact of it since we mainly refer to dictionary
> values only using "keys"  -- but just for curiosity why is this position
> change?
>
> Regards,
> Reuben
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
The key is the way to the value.  Depending on the implementation,
python might find the value by different algorithms.  Sometimes people
call dictionaries 'hashes', which I believe refers to a method of
indexing that does some algorithm on the key to get the value
location.  This is for speed and for space savings in memory.

So, sometimes you might see a dictionary displayed in the order you
entered it, but sometimes not.


-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list