Hash Tables and Hashmaps in Python

Hash Tables and Hashmaps in Python

Last updated on 25th Sep 2020, Artciles, Blog

About author

Praveen (Sr Technical Project Manager - Python )

He is Highly Experienced in Respective Technical Domain with 11+ Years, Also He is a Respective Technical Trainer for Past 5 Years & Share's This Important Articles For us.

(5.0) | 16543 Ratings 469

Hash table or a Hashmap in Python

In computer science,a Hash table or a Hashmap is a type of data structure that maps keys to its value pairs (implement abstract array data types). It basically makes use of a function that computes an index value that in turn holds the elements to be searched, inserted, removed, etc. This makes it easy and fast to access data. In general, hash tables store key-value pairs and the key is generated using a hash function.

Hash tables or has maps in Python are implemented through the built-in dictionary data type. The keys of a dictionary in Python are generated by a hashing function. The elements of a dictionary are not ordered and they can be changed.

An example of a dictionary can be a mapping of employee names and their employee IDs or the names of students along with their student IDs.

Subscribe For Free Demo

Error: Contact form not found.

Difference between Hashtable and Hashmap in Python

Hash TableHashmap
SynchronizedNon-Synchronized
FastSlow
Allows one null key and more than one null valuesDoes not allows null keys or values

Creating Dictionaries

Dictionaries can be created in two ways:

  • Using curly braces ({})
  • Using the dict() function

Using curly braces

Dictionaries in Python can be created using curly braces as follows:

For example:

  • my_dict={‘Dave’ : ‘001’ , ‘Ava’: ‘002’ , ‘Joe’: ‘003’}
  • print(my_dict)
  • type(my_dict)

Output:

  • {‘Dave’: ‘001’, ‘Ava’: ‘002’, ‘Joe’: ‘003’}
  • dict

Using dict() function

Python has a built-in function, dict() that can be used to create dictionaries in Python. 

For example:

  • new_dict=dict()
  • print(new_dict)
  • type(new_dict)

Output:

  • {}
  • dict

In the above example, an empty dictionary is created since no key-value pairs are supplied as a parameter to the dict() function. In case you want to add values, you can do as follows:

For example:

  • new_dict=dict(Dave = ‘001’ , Ava= ‘002’ , Joe= ‘003’)
  • print(new_dict)
  • type(new_dict)

Output:

  • {‘Dave’: ‘001’, ‘Ava’: ‘002’, ‘Joe’: ‘003’}
  • dict

Performing Operations on Hash tables using Dictionaries

There are a number of operations that can be performed on has tables in Python through dictionaries such as:

  • Accessing Values
  • Updating Values
  • Deleting Element

Accessing Values

The values of a dictionary can be accessed in many ways such as:

  • Using key values
  • Using functions
  • Implementing the for loop

Using key values

Dictionary values can be accessed using the key values as follows:

For example:

  • my_dict={‘Dave’ : ‘001’ , ‘Ava’: ‘002’ , ‘Joe’: ‘003’}
  • my_dict[‘Dave’]

Output:

  • ‘001′
Course Curriculum

Attend Hands-on Python Training from Real-Time Experts & Build Your Skills

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

Using functions

There are a number of built-in functions that can be used such as get(), keys(), values(), etc.

For example:

  • my_dict={‘Dave’ : ‘001’ , ‘Ava’: ‘002’ , ‘Joe’: ‘003’}
  • print(my_dict.keys())
  • print(my_dict.values())
  • print(my_dict.get(‘Dave’))

Output:

  • dict_keys([‘Dave’, ‘Ava’, ‘Joe’])
  • dict_values([‘001’, ‘002’, ‘003’])
  • 001

Implementing the for loop

The for loop allows you to access the key-value pairs of a dictionary easily by iterating over them. 

For example:

  • my_dict={‘Dave’ : ‘001’ , ‘Ava’: ‘002’ , ‘Joe’: ‘003’}
  • print(“All keys”)
  • for x in my_dict:
  • print(x) #prints the keys
  • print(“All values”)
  • for x in my_dict.values():
  • print(x) #prints values
  • print(“All keys and values”)
  • for x,y in my_dict.items():
  • print(x, “:” , y) #prints keys and values

Output:

  • All keys
  • Dave
  • Ava
  • Joe
  • All values
  • 001
  • 002
  • 003
  • All keys and values
  • Dave : 001
  • Ava : 002
  • Joe : 003

Updating Values

Dictionaries are mutable data types and therefore, you can update them as and when required. 

If I want to change the ID of the employee named Dave from ‘001’ to ‘004’ and if I want to add another key-value pair to my dictionary, I can do as follows:

For example:

  • my_dict={‘Dave’ : ‘001’ , ‘Ava’: ‘002’ , ‘Joe’: ‘003’}
  • my_dict[‘Dave’] = ‘004’   #Updating the value of Dave
  • my_dict[‘Chris’] = ‘005’  #adding a key-value pair
  • print(my_dict)

Output:

  •  {‘Dave’: ‘004’, ‘Ava’: ‘002’, ‘Joe’: ‘003’, ‘Chris’: ‘005’}
Python Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

Deleting items from a dictionary

There are a number of functions that allow you to delete items from a dictionary such as del(), pop(), popitem(), clear(), etc. 

For example:

  • my_dict={‘Dave’: ‘004’, ‘Ava’: ‘002’, ‘Joe’: ‘003’, ‘Chris’: ‘005’}
  • del my_dict[‘Dave’]  #removes key-value pair of ‘Dave’
  • my_dict.pop(‘Ava’)   #removes the value of ‘Ava’
  • my_dict.popitem() #removes the last inserted item
  • print(my_dict)

Output:

  • {‘Joe’: ‘003’}

The above output shows that all the elements except ‘Joe: 003’ have been removed from the dictionary using the various functions.

Are you looking training with Right Jobs?

Contact Us

Popular Courses