Package: hashed-list
HashedListA List with O(1) time complexity for.index()method, instead of O(N).DescriptionHashedListis a data structure that is pretty much a Python list, except that:The method.index(value)is O(1) (Good)It uses twice more memory due to indexing (Not good but still okay)It takes more time than list during initialization due to hashing of each itemItems must be unique. It will raiseDuplicateValueErrorif
duplicate item is providedMain use case:You have a huge list of unique, ordered items that:You may update the list (remove, insert, set value etc) from
time to timeYou may get the index of a specific item in the list very oftenIn this case, using just a regular list definitely works but will cost
you O(N) each time you get the index of a specific item. Or, you can
maintain along the list a dictionary of item => index, but that will cost
you the burden of updating the dictionary everytime the list is updated.HashedList will make the work easy for you.Installationpipinstallhashed-listUsageSimply instantiate it from an iterable, and use it as you normally would a Python listfromhashed_listimportHashedList# From listhashed_list_1=HashedList(["a","b","c"])# From generatorhashed_list_2=HashedList(xforxinrange(100)ifx%2==0)# From sequencehashed_list_3=HashedList(range(1,100,2))# Exceptionsfromhashed_listimportDuplicateValueErrortry:hashed_list_4=HashedList([1,1,2])exceptDuplicateValueErrorase:print(e)# Use it like a normal Python listhashed_list_1[0]# => "a"len(hashed_list_3)==len(list(range(1,100,2)))# => Truehashed_list_1.index("c")# => 2# The same for .extend(), .append(), .insert(), etc ...Simple benchmarkOn a large list,HashedList.index()should be tens of times faster
thanlist.index()but you can try it yourself by copy-pasting the code below on
your Python shellfromrandomimportrandintimporttimefromhashed_listimportHashedListlist_size=999_999random_values=[randint(list_size//2,list_size)for_inrange(1000)]print("Testing list.index()")t0=time.time()very_huge_list=list(range(list_size))[very_huge_list.index(random_value)forrandom_valueinrandom_values]d1=time.time()-t0delvery_huge_list# Clear up unused memoryprint(f"list.index() took{d1}seconds for{len(random_values)}calls")# => list.index took 7.381884813308716 seconds for 1000 callsprint("Testing HashedList.index()")t0=time.time()very_huge_hashed_list=HashedList(range(list_size))# _ = very_huge_hashed_list.index(random_value)[very_huge_hashed_list.index(random_value)forrandom_valueinrandom_values]d2=time.time()-t0delvery_huge_hashed_list# Clear up unused memoryprint(f"HashList.index() took{d2}seconds for{len(random_values)}calls")# HashList.index took 0.17798161506652832 seconds for 1000 calls# Resultprint(f"HashList.index() is{d1//d2}times faster than list.index()!")# => HashList.index() is 41.0 times faster than list.index()!CaveatsHashedListconsumes 2 times more memory than Python listHashedListconsumes more time during initialization due to hashingGiven these caveats, useHashedArrayonly when you know that.indexis going to be used a lot.DevelopmentTo start developing for and contributing to this repository:# Start a virtual environmentpython-mvenv.venv# Install the package from sourcepython-minstall-e.# Run all testspython-munittestdiscovertests/# Now you can start developing. Please see src/ and tests/# folders for source code and tests respectivelyLinksPYPI