Skip to content

Mastering Hash Tables in Python

[

Build a Hash Table in Python With TDD

Invented over half a century ago, the hash table is a classic data structure that has been fundamental to programming. To this day, it helps solve many real-life problems, such as indexing database tables, caching computed values, or implementing sets. It often comes up in job interviews, and Python uses hash tables all over the place to make name lookups almost instantaneous.

Even though Python comes with its own hash table called dict, it can be helpful to understand how hash tables work behind the curtain. A coding assessment may even task you with building one. This tutorial will walk you through the steps of implementing a hash table from scratch as if there were none in Python. Along the way, you’ll face a few challenges that’ll introduce important concepts and give you an idea of why hash tables are so fast.

In addition to this, you’ll get a hands-on crash course in test-driven development (TDD) and will actively practice it while building your hash table in a step-by-step fashion. You’re not required to have any prior experience with TDD, but at the same time, you won’t get bored even if you do!

Get to Know the Hash Table Data Structure

  • Hash Table vs Dictionary
  • Hash Table: An Array With a Hash Function

Understand the Hash Function

  • Examine Python’s Built-in hash()
  • Dive Deeper Into Python’s hash()
  • Identify Hash Function Properties
  • Compare an Object’s Identity With Its Hash
  • Make Your Own Hash Function

Build a Hash Table Prototype in Python With TDD

  • Take a Crash Course in Test-Driven Development
  • Define a Custom HashTable Class
  • Insert a Key-Value Pair
  • Find a Value by Key
  • Delete a Key-Value Pair
  • Update the Value of an Existing Pair
  • Get the Key-Value Pairs
  • Use Defensive Copying
  • Get the Keys and Values
  • Report the Hash Table’s Length
  • Make the Hash Table Iterable
  • Represent the Hash Table in Text
  • Test the Equality of Hash Tables

Resolve Hash Code Collisions

  • Find Collided Keys Through Linear Probing
  • Use Linear Probing in the HashTable Class
  • Let the Hash Table Resize Automatically
  • Calculate the Load Factor
  • Isolate Collided Keys With Separate Chaining

Retain Insertion Order in a Hash Table

Use Hashable Keys

  • Hashability vs Immutability
  • The Hash-Equal Contract

Conclusion

In this tutorial, you’ll learn:

  • How a hash table differs from a dictionary
  • How you can implement a hash table from scratch in Python
  • How you can deal with hash collisions and other challenges
  • What the desired properties of a hash function are
  • How Python’s hash() works behind the scenes

It’ll help if you’re already familiar with Python dictionaries and have basic knowledge of object-oriented programming.

Please note that this tutorial assumes you have a basic understanding of Python and programming concepts. If you’re new to Python, it’s recommended to start with beginner-level tutorials before diving into hash tables.