[Tutor] Lists or Dictionary?

Jeff Shannon jeff@ccvcorp.com
Mon Jan 20 14:13:02 2003


Tiago Duarte Felix wrote:

> i have already done a class... and experimented with lists.... and it 
> works just fine...
> i need to know if access and modifications to the structure get slower 
> using a dictionar instead of lists....
>  
> it is a big data structure.... 10.000 nodes.... and about 10 
> fields.... (small fields.. numbers and small strings..)


Whether a list or dictionary is preferable depends entirely on what you 
intend to be doing with it.  In both cases, accessing by numeric index 
(for lists) or key (for dictionaries) is very fast, and does not depend 
on the size of the collection.  Adding items is also independent of 
size, as long as you're only adding to the end on lists -- inserting in 
the middle implies shifting all the items after it, which can be slow, 
but dictionaries are unordered so this doesn't apply.

If you are intending to iterate over each node in turn, you need a list. 
 If you will be adding and removing nodes in an arbitrary order, you 
want a dictionary.  Other than that, the decision largely comes down to 
whether it's more convenient for you to index your collection based on 
numbers (list) or more complex (strings, tuples, whatever) keys 
(dictionary).

Jeff Shannon
Technician/Programmer
Credit International