Are lists at least as efficient as dictionaries?

Narendra C. Tulpule naren at trillium.com
Thu Aug 28 20:04:04 EDT 2003


Hi,
  if you know the Python internals, here is a newbie question for you.
If I have a list with 100 elements, each element being a long string,
is it more efficient to maintain it as a dictionary (with a key = a
string from the list and value = None) for the purpose of insertion
and removal?
  Basically, if Python really implements lists as linked lists but
dictionaries as hash tables, it may well be that hashing a key takes
negligible time as compared to comparing it against every list
element.
  Oh and here is (as a non-sequiter) something I don't understand
either:
>>> x = ([],)
>>> x[0] += ['something']
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> x
(['something'],) <---- complained but did it anyway??
>>> x[0].append('and another thing') <------- no complaint!
>>> x
(['something', 'and another thing'],)




More information about the Python-list mailing list