Package: hashlist
DescriptionThis package is meant to store data in a smart way so when you are dealing with large amounts of data it will be faster to query the list and get an answer. For now the package supports only strings as data types but I am working to improve this and allowing for other data types, data structures and objects to be stored.Methodsadd(args*)Can add multiple elements at a time into the hash list objectget_item_index(item)Returns a tuple of form (key, index)get_item_by_index(index)Returns the item by providing the index (key, index)remove_item_by_index(index=None)Removes an item form the hashed list either by it's index. Note that the index should be a tuple of (key, index).remove_item_by_name(item)Removes the item by it's 'name'.delete_key(key)The method deletes a key and all the items associated with it. Be careful how you use this method in your code. The index argument expects an integer (int).keys()The method returns a list of all the keys in the hashed listprint()Prints the hashed list object contents.UsageTo get this module you can install it by using pip as follows:pipinstallhashlistIf you have hashlist installed update it from time to time using this command:pipinstall--upgradehashlistOnce you have your module installed let's create an instance of the class and add some items and print the hash list:fromhashlistimporthast_listh_lst=hast_list()h_lst.add('test','my','python','hashed','list')'''> Output:{112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}'''Note that you can add 1 item at a time or multiple items.The resulting hash list is a dictionary that has stored the words in a key:value pairThe key, for now, is calculated by adding the ASCII values of the letters from the word and dividing the sum to the length of the word:def__key__(self,word):ifword:key=sum([ord(letter)forletterinword])//len(word)returnkeyraiseException('Argument can not be None')You can see all the public methods that the class offers by running this code:fromhashlistimporthast_listmethod_list=[methodformethodindir(hash_list)ifmethod.startswith('_')isFalse]print(method_list)'''> Output:> ['add', 'delete_index', 'get_item_by_index', 'get_item_index', 'indexes', 'print', 'remove_item_by_index', 'remove_item_by_name']'''Ok, great! Now let's use some of those methods. let's start with get_item_index() method.get_item_index(index) will return a tuple of values. The first value in
the tuple is the item's key and the second value will represent the position of the
item in the array:fromhashlistimporthast_listh_lst=hast_list()h_lst.add('test','my','python','hashed','list')item=h_lst.get_item_index('my')print(item)'''> Output:>(115, 0)'''But what if you want to get the actual item (in this case the word). Well we can use the get_item_index() and get_item_by_index():fromhashlistimporthast_listh_lst=hast_list()h_lst.add('test','my','python','hashed','list')index=h_lst.get_item_index('my')item=h_lst.get_item_by_index(index)print(index)print(item)'''> Output:> (115, 0)> my'''Now let's use the keys() method. This method return a list of all the keys that are in your hash listfromhashlistimporthast_listh_lst=hast_list()h_lst.add('test','my','python','hashed','list')all_keys=h_lst.keys()print(all_keys)'''> Output:> [112, 115, 103, 111]'''You can use the delete_key() method to delete a key from the hash list.fromhashlistimporthast_listh_lst=hast_list()h_lst.add('test','my','python','hashed','list')key=h_lst.keys()[0]h_lst.print()h_lst.delete_key(key)h_lst.print()'''> Output:> {112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}> {115: ['my'], 103: ['hashed'], 111: ['list']}'''If you want to delete an item from the hash list but you don't know it's (key, index) you can use the remove_item_by_name()fromhashlistimporthast_listh_lst=hast_list()h_lst.add('test','my','python','hashed','list')h_lst.print()# hash list before removing an itemh_lst.remove_item_by_name('my')h_lst.print()# hash list after removing an item'''> Output:> {112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}> {112: ['test', 'python'], 103: ['hashed'], 111: ['list']}'''If you can see the class automatically deletes the entire key if there is no value associate with it. In this case the word 'my' had the key 115 but because we removed the item the key has no value associate with it so the class deletes the key.You can remove an item by using it's (key, index) by using remove_item_by_key() method:fromhashlistimporthast_listh_lst=hast_list()h_lst.add('test','my','python','hashed','list')h_lst.print()# hash list before removing an itemindex=h_lst.get_item_index('test')print(index)h_lst.remove_item_by_index(index)h_lst.print()# hash list after removing an item'''> Output:> {112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}> (112, 0)> {112: ['python'], 115: ['my'], 103: ['hashed'], 111: ['list']}'''