## DEV Community is a community of 667,450 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Learning Python-Basic course: Day 20, HashTables via Dictionaries

Aatmaj
I'm a Computer Science enthusiast with a keen interest in Maths. I like to tinker with things around me & build projects on my own. I like to play musical instruments & practise Sahaja Yoga meditation

## Hashtables and information retrieval

The central theme behind hash tables is smart information storage and retrieval. The data must be stored in such a manner that it becomes very easy to search it.

Suppose we want to store a data of any thousand numbers ranging from one to a million. Using hashtables, we can efficiently store these numbers in a way which makes it easy for searching. This is possible only if we map the values to 'keys' The idea of hash table is to allow different possibilities of keys that might over to be mapped to the same location in an array. This is done with the help of 'hashfunctions' The hash function takes in a value and maps it to a key.

Example we might take the hash function as taking only the last three digits of the number. So, 45367 will go to the position 367, while 3769 will go to 769. In case of two numbers have same set of last three digits, we can use different algorithms to resolve collision. One way to do that is using 'chained hashtables' or linked hash tables. In this, lists are present in the hashtables instead of numeric values, where the values can be appended. The example below will make things more clear.

In case you are not familiar with hashtables, please visit the following references-

If anyone has more references, then please comment below👇

### Simple hashtable

Let us now see how to make a simple hashtable from using a dictionary. The user enters say five values, and we map them to a table of length ten. If the values of the keys overlap, then error message is generated and new value is ignored. This collisionj management will be handled by the next version of hashtable, i.e. linked hashtables.

For this program, we use a simple hashfunction given be the modulus operator as - `key = value % length`

``````a={}
for i in range(0,5):
key=b%10 #very simple hash function for length 10
try:
int(a[key])
#to check if the element is already present in the position or not.
print("error-- value occupied!")
except:
a[key]=b
print(a)
``````

OUTPUT-

``````Please enter an integer 2
error-- value occupied!
{8: 78, 2: 2, 3: 43, 6: 56}
``````

Exercise 1)-Modify the above code to map Unicode characters to the hashtable.

We now create a linked hashtable, i.e. hashtable of lists. This way, we solve the issue of the keys in the same position. If there are multiple values for the same list, then the values are appended on a list rather than ignoring them. This method is used for fast data retrieval

``````a={}
for i in range(0,10):
key=b%10
try:#append to original list exists.
if key not in a[key]:
a[key].append(b)
except:#make a new list
a[key]=[b]
print(a)
``````
``````Please enter a number 23
{3: [23, 123], 4: [34, 54], 5: [45, 65], 6: [86], 7: [457], 9: [79, 389]}
``````

### Retrieving data from the hashtable

We can retrieve the data from the hashtable very efficiently. In the same way as we added the data, we now will remove and display it.

``````b=int(input("Please enter the value"))
key=b%10
print(a[key].pop(b))
``````

Answers as usual in the learning python repository

Hashtables are one of the many many data structures present. For more information related to data structures and algorithms, please visit this series of blogs-Data structures blog series by
Aya Bouchiha on Dev.to

✌️So friends that's all for now.