[Tutor] Lists or Dictionary?

Gonçalo Rodrigues op73418@mail.telepac.pt
Mon Jan 20 12:13:09 2003


----- Original Message -----
From: Tiago Duarte Felix
To: Tutor@python.org
Sent: Monday, January 20, 2003 4:25 PM
Subject: [Tutor] Lists or Dictionary?

>i need to creata a data structure... i don't know what to use... it should
be something like this:
>
>node[0].name
>node[0].state
>
>or this:
>
>node['A'].name
>node['A'].state
>
>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....

List acess-by-index is O(1) as is dictionary acess-by-key. Since by the
above, you can have keys that are strings this seems to rule out lists.

>
>it is a big data structure.... 10.000 nodes.... and about 10 fields....
(small fields.. numbers and small strings..)

A dictionary of nodes, probably? Encapsulate your "small fields" in a Node
(or whatever you want to name it) class and then stuff it's instances in the
dictionary. Although 10000 * 10 attributes of "numbers and small strings"
does not seem a lot for today's memory standards you could consider the
__slots__ feature (Python >= 2.2) to save some memory.

If  you need an extended interface on the dictionary you could also subclass
dict (Python >= 2.2 ). To say more, more info is needed.

With my best regards,
G. Rodrigues