Dictionary in Data Structure
![DSA Problem Solving for Interviews using Java](https://www.scaler.com/topics/images/jitender_punia.webp)
Overview
A dictionary in data structure is used to store the data in the form of key-value pair. The dictionary in data structure has an attribute called the key. The key is the attribute that helps us locate the data or value in the memory. A dictionary in data structure supports a wide variety of operations using a wide variety of methods and functions. The keys are always unique within a dictionary. The values of the dictionary in the data structure may or may not be unique. We can put heterogeneous type values inside the dictionaries.
As the keys are unique (no two keys can be the same in a dictionary data structure), we can easily obtain the values related to the specific key. Dictionary in data structures is referred to with various names such as maps, symbol tables, etc.
What is a Dictionary in Data Structure?
Before learning about the dictionary in data structure, let us first learn what is a data structure in programming and how it is related with the data types.
The data structures are nothing but a meaningful way of arranging, and storing the data in the computer for efficient utilization and processing depending upon the situation. The data structure is one of the most fundamental topics of any programming language.
Apart from the data structures, we also have data types in programming languages. Data types are a classification of the data that helps us to distinguish between various categories of values. Data structures are a systematic collection of these data types. Some of the most commonly used data types are integer, character, float, double, bytes, etc.
The data structures can be broadly divided into two categories. The first one is the built-in data structures, and the second one is user-defined data structures. Some of the most commonly used data structures are - lists, dictionaries, tuples, sets,. stacks, queues, trees, linked lists, graphs, hashmaps, etc.
Note: Dictionary in data structure can be found as built-in data types in various programming languages like Python, C#, etc.
The dictionary in data structure is an unordered collection of data items that are used to store the data in the form of key-value pairs. The dictionary data structure has an attribute called the key which helps us locate the data or value in the memory. A dictionary in data structure can be compared with our regular language dictionary where a word acts like a key and its meaning(s) acts like value(s).
As we can see in a real-world dictionary, a word can have various meanings similarly, a key can be used to refer to various values. So, the value of a dictionary in data structure can be an immutable collection of objects or simply a single value as well.
Apart from that, the keys of a dictionary are unique and similar to our real-world dictionary. Since the keys are unique (no two keys can be the same in a dictionary data structure), we can easily obtain the values related to the specific key.
A key attribute of the dictionary in data structure follows two rules. Let us briefly discuss them.
- The key attribute of the dictionary data structure must be unique and consist of a single element. As we have discussed above, the unique nature helps us in faster and easy retrieval of the corresponding set of values.
- As no duplicate keys are allowed in the dictionary data structure, whenever a duplicate key is found, the last assigned value is treated as the final key-value pair. So, if we insert duplicate keys then our original data is lost.
- The key attribute is case sensitive hence "KEY" is not the same as "key".
- The key attribute is immutable hence, we cannot have an array or list as keys we can only have numbers, strings, etc, as the key.
The value part or attribute of the dictionary in data structure is simpler to define with lesser rules imposed on it. The value part can be a single value or a collection of values stored in any sequential data type like lists, tuples, sets, etc.
Note: Dictionary in data structures are referred to by various names such as maps, symbol tables, etc.
The dictionary in data structure is usually created using the curly braces as {key: value(s)}. Now, the dictionary's key-value pair is first converted into some hash value (using a hash function). This hash code is then stored in buckets in the memory. The hash code helps in faster data retrieval as they are always unique.
Refer to the image for better visualization of the dictionary data structure.
The dictionary data structure is widely used in the field of programming (Refer to the section Usage of the dictionary data structure in different programming languages for detailed discussion). Since it is a widely used data structure, the programming languages have various methods or functions to support the associated operations of dictionaries in the data structure.
Let us discuss some of the most commonly used operations of dictionary in data structure
Operations of Dictionary in Data Structure
As we know data structures are used to store values so that they can be easily used, manipulated, and retrieved. Similarly, a dictionary in data structure is used for storing data in key-value pairs. Let us now discuss the various operation that can be performed on the dictionary in the data structure.
- Data insertion: It is quite obvious, that every data structure stores the data inserted into it. The important point to note here is that the key attribute can be assigned only immutable data like numbers, strings, etc. On the other hand, the value attribute can store a single value, an object, or a sequence of values inside other data structures like lists, tuples, stacks, sets, etc. The data is only inserted into a dictionary data structure when there is no such key-value pair already existing in the dictionary. Now, if a key-value pair is already inserted previously then the old key-value pair will be overridden by the new key-value pair.
- Data updation: The value of any data element can be easily updated. We first need to access the value using a key attribute and then update the corresponding value(s). We can access the value of a particular by using the dictionary[key] statement.
- Data deletion or removal: We can also delete a key-value pair in a dictionary by accessing the key attribute. We should know that if a key is not present in the dictionary and we are trying to delete the key value then an error is shown by programming languages like Python, C#, etc.
- Data verification: We can also verify or check where a particular key exists in the dictionary or not.
The data items of a dictionary are unordered. So, we can use various loops to iterate over the key-value pairs of the data. Many programming languages offer various methods for easier iteration of keys and values.
Usage of the Dictionary Data Structure in Different Programming Languages
As we know the data in the dictionary data structure is stored in key-value mappings. The dictionary in data structure internally uses hashing which makes the data retrieval faster. The hashing uses a special function called function to create hash code. As these hash codes are unique for each data (key-value) pair so we can easily retrieve data in O(1) time in the best cases.
The dictionary data structure can store a large set of values for a specific key. Hence it is widely used to store a heterogeneous set of data for a key. We can also create a dictionary inside another dictionary.
For example, all the data of an employee can be stored using a dictionary in data structure. Since the key needs to be unique, we can use the unique employee ID as the key and various details (such as name, designation, mobile number, address, salary, etc.) as values.
For better clarity on the dictionary in data structure, let us take a simple example of a dictionary in Python programming language.
Output:
Conclusion
- The dictionary in data structure is used to store the data in the form of key-value pair.
- The key is the attribute that helps us locate the data or value in the memory.
- The keys are always unique within a dictionary. The values of the dictionary in data structure may or may not be unique.
- Dictionary in data structures are referred to by various names such as maps, symbol tables, etc.
- No duplicate keys are allowed in the dictionary data structure, so whenever a duplicate key is found, the last assigned value is treated as the final key-value pair.
- The key attribute is case sensitive hence "KEY" is not the same as "key".
- The key attribute is immutable hence, we cannot have an array or list as keys we can only have numbers, strings, etc, as the key.
- The value attribute can contain a single value or a collection of values stored in any sequential data type like lists, tuples, sets, etc.
- The dictionary in data structure internally uses hashing which makes the data retrieval faster.
- The dictionary data structure can store a large set of values for a specific key so it is widely used to store a heterogeneous set of data for a key.