list vs. dict

Max M maxm at mxm.dk
Wed Feb 27 10:27:52 EST 2002


Beda Kosata wrote:

> for a piece of code I need to store relatively large amount of records 
> (100-1000). Each record will contain few (10-20) pieces of information 
> (mostly strings or integers).


Unless those are very long strings this is a pretty small dataset.

> For convenience it would be better for me to make this records as dicts, 
> so I don't have to remember what data has what position in the list. 


First you should write it the way that is easiest for you (and others) 
to read and modify. That is the most important thing.

If, and only if speed, still is an issue then do you rewrite.

> However the most important for me is speed and I suppose that working 
> with lists is faster than with dicts. So my question is - how much do 
> lists and dicts differ in speed?


You really should test it with a simulated data set if it is that important.

> Does anyone have some experience?

It usiallu doesn't matter for business logic etc. but if it is 
computational intensive it might.

btw: if you really want to store it in a list you can get dict-like 
behaviour with list speed like this:

keys = (
     NAME, AGE, SEX
     ) = range(3)
def pseudoDict():
     return [None]*len(keys)

items = []

person = pseudoDict()
person[NAME] = 'Max M'
person[AGE]  = 36
person[SEX]  = 'Male'
items.append(person)

# or

person2 = []
person2.append('Somebody') # NAME
person2.append(29)         # AGE
person2.append('Male')     # SEX
items.append(person2)


for item in items:
     print item[NAME]


But I don't think it will be nessecary.

regards Max M




More information about the Python-list mailing list