When working with collections of elements, like a list of strings, or a list of tuples, it becomes computationally very expensive to search for elements in the collection or answer queries like “Is item X present in our collection?”. Yet, comparing numbers is very fast, and our computers are specialized in comparing numbers very efficiently (unlike strings, or tuples). So, if we can map the data at hand to a numeric value, it would make some operations like finding it, or checking if it’s present in the collection quicker.
The technique of mapping data of arbitrary size to fixed-size data is called hashing. The resulting number (hash) serves as a unique identifier for the original data. We are usually given a hash function that can map the initial data to fixed-sized data (like a number).
Python, for instance, has a built-in hash function that can map any immutable object to a number, which is usually in the range . Note that it can only map immutable objects. So, trying to hash a list or a dictionary (which are mutable and can be changed by adding or removing elements) would result in an error: