I made a little benchmark and got into a conclusion that List works faster.
Here is the benchmark I've made:
Running with list: 198sec user time:
> time python /tmp/python_benchmark.py list
finished_building
198.816u 0.036s 3:18.97 99.9% 0+0k 0+0io 0pf+0w
Running with dictionary: 211sec user time:
> time python /tmp/python_benchmark.py dict
finished_building
211.065u 0.044s 3:31.19 99.9% 0+0k 0+8io 0pf+0w
I've printed the finished building string, to know how much approx. seconds it took to build the data structure, before starting to run the queries. In both times, it took less that 1 second.
This is the content of the benchmark script:
import sys
from random import randrange
NUM_OF_ITEMS = 100000
NUM_OF_ACCESSES = 200000000
data_structure = None
def exit_usage():
print "Usage: python_benchmark.py <list | dict>"
sys.exit(1)
def access_data_structure():
for i in xrange(NUM_OF_ACCESSES):
rnd_index = randrange(NUM_OF_ITEMS)
dummy_assignment = data_structure[rnd_index]
if len(sys.argv) != 2:
exit_usage()
work_type = sys.argv[1]
if work_type != 'list' and work_type != 'dict':
exit_usage()
if work_type == 'list':
data_structure = []
else:
data_structure = {}
if work_type == 'list':
for i in xrange(NUM_OF_ITEMS):
data_structure.append(i)
else:
for i in xrange(NUM_OF_ITEMS):
data_structure[i] = i
print 'finished_building'
access_data_structure()
O(1)in the average case - see wiki.python.org/moin/TimeComplexity. If you will have values for all 10,000 indices, alistmakes more sense; if it will be sparse, the dictionary may use less space.listclass is a dynamic array, not a linked list.__hash__on your own class) you would get an O(n) dict :)