Updated April 17, 2023
Introduction to Hash table in Python
The hash table in python is used to store the data through key-value pairs; it is implemented by using the dictionary. A Hash table is one of the data structures that store the data in the form of key-value pair. Every value has a unique key which is generated through a hash function, which makes accessing the data or value in the hash table faster, regardless of the number of values present in the hash table.
Dictionary is the built-in data type in python, with the help of which implements the hash table in the python. A hashing function is used in Python to generate the keys of a dictionary. A dictionary’s keys are not ordered and can be modified.
Syntax
The syntax to create the dictionary in python –
d = { key : value }
or
d = dict( key = value )
The dictionary can be created in python by using the braces “{“ and “}” and also can be created by using the dict() function. When creating a dictionary by using the curly braces, the key and value are separated by the “:” and each pair separated by the “,”. Whereas dict() is the built-in function to create the dictionary using dict() function, the key and value are separated by the “=”, and each key-value pairs are separated by the “,”.
Working of hash table in python
The hash table in python can be created with the help of the dictionary, and the dictionary can be created by using the curly braces and dict() function. Suppose we have a dictionary of the student marks which contains the student marks. Now we need to update the student marks data, so we can perform as “Student_marks[56] = 65”, where Student_marks is the dictionary, and 56 is the key stored in the dictionary for the student of roll no 56. So it updates the marks value 65 for roll no 56.
Examples of hash table in python
Given below are the examples of hash table in pyhton:
Example #1
Example for a hash table in python to create and access the dictionary values –
Code:
# declare and initialize the dictionary
d = { 'Name' : 'John', 'Rollno' : 34, 'Marks' : 89}
# Access the value of the dictionary with the key
print( "d['Name'] : ", d['Name'])
print( "d['Rollno'] : ", d['Rollno'])
print( "d['Marks'] : ", d['Marks'])
# passing the non-available key in the dictionary
print( "d['DOB'] : ", d['DOB'])
An output of the above code is –
As in the above program, the dictionary for the student record is created that contains the name, roll no, and marks as the keys. Next, access the values of the dictionary with the help of keys as “d[‘Name’]” where “d” is the dictionary, and “Name” is the key. Next, trying to access the value from the dictionary with the key that is not present in the dictionary, which through the “keyError” as we can see in the above output.
Example #2
Example for a hash table in python to delete the dictionary key-value pair –
Code:
# declare and initialize the dictionary
Fruits_count = { 'apple' : 50, 'banana' : 34, 'orange' : 89, 'cherry' : 78}
# Access the value of the dictionary with the key
print( "Fruits_count['apple'] : ", Fruits_count['apple'])
print( "Fruits_count['cherry'] : ", Fruits_count['cherry'])
print( "Fruits_count['orange'] : ", Fruits_count['orange'])
# delete the key value pair in dictionary with key
del Fruits_count['apple']
# printing the dictionary
print( Fruits_count)
# deleting the non-available key in the dictionary
del Fruits_count['mango']
An output of the above code is –
As in the above program, the dictionary for the fruits count is created that contains the name of the fruits as the keys and the respectively available quantity as the values. Next, access the values of the dictionary with the help of keys as “Fruits_count[‘apple’]” where “Fruits_count” is the dictionary, and “apple” is the key. Next, deleting the key-value from the dictionary with the key as “del Fruits_count[‘mango’]” where del is the keyword that deletes the element. Next, trying to delete the key-value from the dictionary with the key that is not present in the dictionary, which through the “keyError” as we can see in the above output.
Example #3
Example for a hash table in python to update the dictionary values –
Code:
# declare and initialize the dictionary
Student_marks = { 12 : 59, 56 : 60, 18 : 89, 27 :78, 30 : 68}
# Access the value of the dictionary with the key
print( "Student_marks[56] : ", Student_marks[56] )
print( "Student_marks[12] : ", Student_marks[12] )
print( "Student_marks[27] : ", Student_marks[27] )
# update the value in dictionary with key
Student_marks[56] = 65
# add new key value pair
Student_marks[14] = 70
# printing the dictionary
print( Student_marks )
An output of the above code is –
As in the above program, the dictionary for the student marks is created that contains the roll numbers of the students as the keys and the respective marks as the values. Next, access the values of the dictionary with the help of keys as “Student_marks[56]” where “Student_marks” is the dictionary, and “56” is the key. Next, updating the value of the dictionary with the key as “Student_marks[56] = 65”, which updates the marks 65 for the key 56. Next, add the new key-value pair as “Student_marks[14] = 70”, which adds new key 14 and value as 70 to the Student_marks dictionary, as we can see in the above output.
Conclusion
The hash table in python is implemented by using the dictionary, which is used to store the data through key-value pairs.
Recommended Articles
This is a guide to the Hash table in Python. Here we discuss the introduction, syntax, and working of a hash table in python along with examples and outputs. You may also have a look at the following articles to learn more –