dictionary lookup table?

J

John Mudd

I must be missing something here. It's clearly faster to lookup an item
directly in a dictionary than to scan through a list. So when I have a
large lookup table I always load it in the form of a dictionary. But it
seems a waste. I end up having to assign an artificial value to the
dictionary entry. Below I assign the value "None" to each dictionary
entry.

Is there a better way? I feel like I'm misusing the dictionary.



#! /usr/bin/env python
import random
import time

dict = {}
list = []

n = 10000
n = 10000
for i in range(n):
str = '%s' % random.random()
list.append(str)
dict[str] = None # <-- seems a waste

sortedList = list[:]
sortedList.sort()

t1 = time.time()
for i in range(n):
if sortedList not in list:
print 'not found'
print 'List lookups: %.3f sec' % (time.time() - t1)

t1 = time.time()
for i in range(n):
if not dict.has_key(sortedList):
print 'not found'
print 'List lookups: %.3f sec' % (time.time() - t1)
~
~
~
~
~
:!lookup.py
List lookups: 28.848 sec
List lookups: 0.041 sec

Hit ENTER or type command to continue
 
F

Francis Avila

John Mudd said:
I must be missing something here. It's clearly faster to lookup an item
directly in a dictionary than to scan through a list. So when I have a
large lookup table I always load it in the form of a dictionary. But it
seems a waste. I end up having to assign an artificial value to the
dictionary entry. Below I assign the value "None" to each dictionary
entry.

Is there a better way? I feel like I'm misusing the dictionary.

You ARE misusing both dictionaries AND lists, but not in the way you think:
dict = {}
list = []

These lines obliterates the dict() and list() builtins! Use different
names, like D, mydict, etc.

Now, to answer your question:
I don't think you are abusing the dictionary at all, but if you're using
Python 2.3, you can use the set module, which more closely matches the
semantics of your problem. However, as of now the set module is implemented
with dictionaries anyway, so I don't see that you gain much. (I think set
will be implemented as an extension later, though.)

Further, don't be too worried about "waste", because there's very little
waste here. First of all, None is a singleton (hence the Python idiom
"something is None"), so there's no danger of Python possibly making
multiple copies of the item mapped to your key. Secondly, dictionaries are
containers, like lists, so the key and item of a dictionary are just
references to an object. You end up with (at most) twice as many references
in a dictionary as an equivalent list, but references are little more than
dressed-up pointers, and very cheap to store and use. So what are you
worried about?

Finally, if you're worried about waste, why are you using Python? ;) Python
saves your development time in exchange for the far cheaper resource of
memory and processor power.

If you want some more speed (although it looks plenty fast to me), try
experimenting with some other dictionary idioms. Try "if key in
dictionary", "try: D[key]; except: print 'not found'", etc. But I think
you'd be wasting more time than you'd gain, especially since optimization
obsessions can be hard to shake....
 
D

David Eppstein

"Francis Avila said:
I don't think you are abusing the dictionary at all, but if you're using
Python 2.3, you can use the set module, which more closely matches the
semantics of your problem. However, as of now the set module is implemented
with dictionaries anyway, so I don't see that you gain much.

You gain in expressing more clearly the intent of your code.
 
F

Francis Avila

David Eppstein said:
You gain in expressing more clearly the intent of your code.

I'm sorry, I meant "gain" in terms of performance/space, per the OP's
original concern. You are correct, however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top