[Python-ideas] matrix operations on dict :)

Steven D'Aprano steve at pearwood.info
Thu Feb 9 01:51:17 CET 2012

Sturla Molden wrote:
> On 08.02.2012 13:12, julien tayon wrote:

>> It is stupid to code matrix with an hash, I just say as there is a
>> strong analogy between dict and vectors,
> No there is not. A vector is ordered, a hash-table (dict) is unordered.
> - In a vectorlike structure, e.g. a Python list, element i+1 is stored 
> subsequently to element i.

Not necessarily. There is nothing in the API for Python lists that *requires* 
that elements are stored in one continuous array. That's a side-effect of the 

> - In a hash-table, e.g. a Python dict, element hash(i+1) is not stored 
> subsequently to element hash(i).

You are focusing too much on accidental implementation details and not enough 
on the fundamental concept of "vector" or "hash table".

Fundamentally, a "dict" is a data structure that associates arbitrary keys to 
values, such that each key is unique but values may not be. Note that the use 
of a hash table for dicts (mappings) is just one possible implementation.

Fundamentally a list is a mapping from sequential (and therefore unique) 
integer keys (the indexes) to values. Note that a linear array with the key 
(index) being implicit rather than explicit is just one possible implementation.

I have no opinion on whether Julien's proposal is useful or not, but vectors 
(lists) can be implemented using mappings (dicts). Lua is proof of this: their 
table type operates as both list and dict.



More information about the Python-ideas mailing list