[Tutor] Re: [Tutor] Lists or Dictionary?

Magnus Lycka magnus@thinkware.se
Mon Jan 20 18:30:02 2003


Tiago Duarte Felix wrote:
> i need to creata a data structure... i don't know what to use... it shoul=
d be something like this:
...

I'm not sure what your structure looks like, but lists and dicts are
very different. With a dict you can use any unique key that you like.
With a list containing n elements, your "keys" will be the integers
0 to n-1. If you remove element x, all objects with locations higher
in the list than x will shift keys, unless you just leave an empty 
space in the list (myList[x] = None), but then you might end up with 
a big list full of holes, not very effective. So, if you need to 
maintain some kind of searchable object identifiers, dicts are nice,
but if you use lists you might end up having to make a linear search
for your objects. :(
 
> i have already done a class... and experimented with lists.... and it wor=
ks just fine... 
As Alan said: If it works, why change it? That is...if it
works with the amount of data it's intended for...

> i need to know if access and modifications to the structure get slower us=
ing a dictionar instead of lists....

Access is fast in both cases. Modification is always fast 
for dictionaries. On insert and removal lists get slower
the further away from the tail of the list that you get. 
You can easily test this.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se