[Tutor] A couple of somewhat esoteric questions

Alan Gauld alan.gauld at btinternet.com
Wed Oct 22 11:59:10 CEST 2014


On 22/10/14 00:54, Clayton Kirkwood wrote:
> As I’ve contemplated the usage of dictionaries, I face the question of
> efficiency.

Don;t worry about it. Python dictionaries are highly efficient and used 
throughout the language. namespaces and classes are both effectively 
dictionaries so every time you use a variable name or class attribute 
(whether your own or a built-in) you are using a dictionary.

> remember correctly dictionaries(assoc. arrays), having hashes, are
> efficient for storing sparse arrays

They are sparce arrays using hashing for lookup.

> traversal of the dictionary looking for the proper key, and being much
> less efficient if looking for the value and trying to retrieve its key.

Indeed, that's why dictionaries don't have a get key for
value operation.

> But it also depends on the hash key and algorithm and how many spaces
> are “available” for filling.

Yes, and Python's hashing has been tuned and optimised over many years.

> I’ve not had much use of dictionaries in the past so maybe I am missing
> something.

You are using them all the time. Python is built on dictionaries.
They are very powerful and efficient.

> As I contemplated dictionary use, it seems changing the order
> of entries is best left to double single arrays

There is no order. The order may change in use and you have
no control over that. It's a Python internal detail and you as a 
programmer cannot mess with it.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list