Why Do We Need A dict?

Dictionary came late into the game. It developed from "set" the mother of all collection types.
One striking thing about a set is that it always contains unique objects. The object is supposed to contain mechanisms inside it to implement that single most important property of the set. That property is leveraged in many ways in programming.
In Python, the simplest way to get the unique values out of a "list" is to do this:
set(list)
How does the function determine the uniqueness of complex objects? For simple objects like integer or float, we can use the equality check operator - ==. What about the complex objects?
Python runs hash function on every object and uses the resultant hash values to compare the objects. Hash functions yield sufficiently large integers to avoid any collision.
In Python, a test code looks like this:

Now consider your object to be a little more complex than the example above.

You have created and loaded 10,000 person objects into a set object. Now in the course of writing code, you come across a need for finding the relate object for a person by his phone number.
In the current scenario, you need to iterate over all the 10,000 members of the set and compare the phone number of each with the one you have got as an input parameter. That is quite a tedious task!
But the same task becomes considerably simple when you store the same objects in a dict with the key as phone number, value as a tuple as usual.
So you have something like this:
person_dict={6789043345: (..,..), 8764369090: (..,..).......}
When you get a phone number, phno as input, you just have to do person_dict[phno] to get the entire record.
Set evolved into dict to serve a specific business purpose.

But the business needs continued to grow. Dict alone could not handle all the use cases. So we went on sub-classing dict to create different variants like DefaultDict, OrderedDict, MultiDict, etc.
Before we had dict, we had only one set. Now, after moving to dict, a new set besides the existing one got created. It has all the keys as its members.
So it further reduces to a problem of domain mapping - that of mapping members of one domain to the other. For that very reason, in other programming languages, we often refer the dictionary to as a map. Java has Map as an interface class and HashMap as implementation.

May 11th, 2022