In this Python tutorial, we will see what is a hash table, and also we will see the use of a hash table in python.
What is the Hash table in python?
In python, the Hash table is a type of data structure that maps keys to its value pairs. It makes accessing data faster as the index value behaves as a key for data value.
It is implemented through built-in dictionary data type.
Creating Dictionaries in python
Dictionary in python is represented by curly braces. So, for creating a dictionary we can use the curly braces also we can use the dict() function for passing key-value pairs as parameters.
Example:
my_dictionary = {'avinan': '111', 'john': '222', 'nick': '223'}
print(my_dictionary)
After writing the above code (creating Dictionaries in python), Ones you will print “my_dictionary” then the output will appear as a “{‘avinav’: ‘111’, ‘john’: ‘222’, ‘nick’: ‘223’}“. Here, my dictionary with key-values has been printed.
You can refer to the below screenshot creating dictionaries in python.
Also, we will see how to use dict() function for creating a dictionary in python.
Example:
my_dict = dict(avinav = '111', john = '222')
print(my_dict)
After writing the above code (creating Dictionaries in python by using dict() function), Ones you will print “my_dict” then the output will appear as a “{‘avinav’: ‘111’, ‘john’: ‘222’}“. Here, we used the dict() function by passing the parameters and my dictionary is created by those parameters.
You can refer to the below screenshot creating dictionaries in python by using dict() function.
Accessing values in the dictionary
In python, dictionary values can be accessed by using key to obtain the values.
Example:
my_dictionary = {'avinan': '111', 'john': '222'}
print(my_dictionary['john'])
After writing the above code (accessing values in the dictionary), Ones you will print “my_dictionary[‘john’]” then the output will appear as a “222“. Here, we obtain the value of the specified key.
You can refer to the below screenshot accessing values in the dictionary
Updating values in a dictionary
In python, we can update dictionary when required as dictionary is mutable data types.
Example:
my_dictionary = {'avinav': '111', 'john': '222'}
my_dictionary['john'] = '221'
print(my_dictionary)
After writing the above code (updating values in a dictionary), Ones you will print “my_dictionary” then the output will appear as a “{‘avinav’: ‘111’, ‘john’: ‘221’}“. Here, we updated the value of the key and now the ‘john’ value is 221.
You can refer to the below screenshot updating values in a dictionary
Deleting items from the dictionary
In python, we can use del() function to delete any item from the dictionary.
Example:
my_dictionary = {'avinav': '111', 'john': '222'}
del my_dictionary['avinav']
print(my_dictionary)
After writing the above code (deleting items from the dictionary), Ones you will print “my_dictionary” then the output will appear as a “{‘john’: ‘221’}“. Here, we use the del() function to delete ‘avinav’ from the dictionary.
You can refer to the below screenshot deleting items from the dictionary
This is how we can use the Hash table in python.
You may like the following Python tutorials:
- Python Hello World Program (using Pycharm)
- 11 Python list methods
- Python Palindrome Program
- How to create a list in Python
- Block Indentation in Python
- Python For Loop with Examples
- Python read excel file and Write to Excel in Python
- Create a tuple in Python
- Python create empty set
- Python pass by reference or value with examples
- Python select from a list + Examples
In this tutorial, we discussed the Hash table in python, and also we have seen how to create it by using a dictionary.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.