How much slower is dict indexing vs. list indexing?

Emin emin.shopper at gmail.com
Fri Jan 12 09:10:39 EST 2007


On Jan 11, 5:53 pm, Steve Holden <s... at holdenweb.com> wrote:
>
> What technique were you thinking of to look up the cached index values
> in Python, just as a matter of curiosity? Storing them in a dict? It
> would be hard to think of a faster way ... ;-)

I didn't have anything fancy in mind. I was just wondering whether it
makes sense to replace a block of code like

data = {'a' : 1, 'b' :2, 'c' : 3}
for i in someLargeList:
   for name in ['a','b','c']:
      result.append(data[name])

with somthing like

data = {'a' : 1, 'b' :2, 'c' : 3}
dataValues = [data[k] for k in ['a','b','c']
for i in someLargeList:
   for index in [0,1,2]:
      result.append(dataValues[index])

It sounds like it's probably not worth the effort in general, but might
be for extremely ultra-critical parts of code.

Thanks




More information about the Python-list mailing list