Hash table in python

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.

Creating Dictionaries in python
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.

Creating Dictionaries in python
Creating Dictionaries in python

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

Accessing values in the dictionary
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

Updating values in a dictionary
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

Deleting items from the dictionary
Deleting items from the dictionary

This is how we can use the Hash table in python.

You may like the following Python tutorials:

In this tutorial, we discussed the Hash table in python, and also we have seen how to create it by using a dictionary.