Javatpoint Logo
Javatpoint Logo

Dictionary Data Structure

Dictionary is one of the important Data Structures that is usually used to store data in the key-value format. Each element presents in a dictionary data structure compulsorily have a key and some value is associated with that particular key. In other words, we can also say that Dictionary data structure is used to store the data in key-value pairs. Other names for the Dictionary data structure are associative array, map, symbol table but broadly it is referred to as Dictionary.

A dictionary or associative array is a general-purpose data structure that is used for the storage of a group of objects.

Many popular languages add Dictionary or associative array as a primitive data type in their languages while other languages which don't consider Dictionary or associative array as a primitive data type have included Dictionary or associative array in their software libraries. A direct form of hardware-level support for the Dictionary or associative array is Content-addressable memory.

In Dictionary or associative array, the relation or association between the key and the value is known as the mapping. We can say that each value in the dictionary is mapped to a particular key present in the dictionary or vice-versa.

The various operations that are performed on a Dictionary or associative array are:

  • Add or Insert: In the Add or Insert operation, a new pair of keys and values is added in the Dictionary or associative array object.
  • Replace or reassign: In the Replace or reassign operation, the already existing value that is associated with a key is changed or modified. In other words, a new value is mapped to an already existing key.
  • Delete or remove: In the Delete or remove operation, the already present element is unmapped from the Dictionary or associative array object.
  • Find or Lookup: In the Find or Lookup operation, the value associated with a key is searched by passing the key as a search argument.

Now, let us see the usage of the dictionary data structure in different programming languages.

Python:

A sample python code to perform all the basic four operations like create, update, delete and update.

Code:

Output:

Enter the jersey number to be entered
45
Enter the player name to be entered
Rohit Sharma
{45: 'Rohit Sharma'}
Enter the jersey number to be entered
18
Enter the player name to be entered
Virat Kholi
Enter the jersey number to be entered
7
Enter the player name to be entered
Mahendra Singh Dhoni
Enter the jersey number to be entered
42
Enter the player name to be entered
Shikar Dhawan
{45: 'Rohit Sharma', 18: 'Virat Kholi', 7: 'Mahendra Singh Dhoni', 42: 'Shikar Dhawan'}

Enter the key whose value you want to update
42
Enter the new value that you want to assign to the key entered
Shikhar Dhawan
The dictionary after updating the values is:
{45: 'Rohit Sharma', 18: 'Virat Kholi', 7: 'Mahendra Singh Dhoni', 42: 'Shikhar Dhawan'}

Enter the key that you want to delete or remove from the dictionary object
18
The dictionary after deleting the values is:
{45: 'Rohit Sharma', 7: 'Mahendra Singh Dhoni', 42: 'Shikhar Dhawan'}

In this code, we have created a dictionary object named players having key as integer and value as

Strings. We have also created functions to implement all the basic four functionalities of the dictionary that are create, delete, update, etc.

As we can see in the output of the above code, we have 4 elements as input to our dictionary object created and then update one value in the dictionary and then we deleted one value in the dictionary and in the last we have printed the final dictionary object.

Java

A sample java code to perform all the basic operations like create, delete and update.

Code:

Output:

Enter the key for the Dictionary Object
name
Enter the value for the Dictionary Object
Nirnay Khajuria
Enter the key for the Dictionary Object
age
Enter the value for the Dictionary Object
23
{age=23, name=Nirnay Khajuria}


Enter the key of the element that you want to remove or delete 
age
Dictionary after removing the element(s)
{name=Nirnay Khajuria}


Enter the key of the element that you want to search or find 
name
The value of the key name is Nirnay Khajuria

In this code, we have created a dictionary object named dictObject having key as string and value as

Strings. We have also created functions to implement all the basic four functionalities of the dictionary that are the create, delete, update, search, etc.

As we can see in the output of the above code, we have two elements as input to our dictionary object created and then update one value in the dictionary and then we searched one value in the dictionary and in the last we have printed the final dictionary object that we have created.

CPP:

Now let us write a C++ code that will give us an idea about how to use Dictionary or associative array and their basic functionalities in C++.

Code:

Output:

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : India
Enter the capital of India : Delhi

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : Dominica
Enter the capital of Dominica : Roseau

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : Haiti
Enter the capital of Haiti : Port-au-prince

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:1

Enter the name of the country : USA
Enter the capital of USA : Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:2

Contents of the Dictionary are : 
Name of the country Dominica: Name of the capital Roseau
Name of the country Haiti: Name of the capital Port-au-prince
Name of the country India: Name of the capital Delhi
Name of the country USA: Name of the capital Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:3


Enter the name of the country that you want to delete : Haiti
Element deleted successfully.

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:2

Contents of the Dictionary are : 
Name of the country Dominica: Name of the capital Roseau
Name of the country India: Name of the capital Delhi
Name of the country USA: Name of the capital Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:4

Result of the search in the dictionary is  : 
Enter the name of the country that you want to search : USA
Capital of USA is Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:5


Enter the name of the country whose capital you want to update : India
Enter the name of new capital : New-Delhi       
Contents of the Dictionary updated sucessfully.

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:2

Contents of the Dictionary are : 
Name of the country Dominica: Name of the capital Roseau
Name of the country India: Name of the capital New-Delhi
Name of the country USA: Name of the capital Washington

1. To insert data into the Dictionary.
2. To print data from the Dictionary.
3. To delete data from the Dictionary.
4. To search data from the Dictionary.
5. To update data from the Dictionary.
0. To exit the code.
Enter your choice:0

As we can see in the above code, we have successfully implemented all the basic operations that are search, update, insert, delete and print an object of dictionary named capitals that we have created to store the name of the countries and their respective capitals.

Different functions are created for all of the operations that are mentioned above. First, we created a dictionary and added elements to this created object. Once the insertion of the data is completed in the object, we displayed the added data. After displaying, we deleted already existing data from the dictionary object. After deletion, a searching and updation operations are performed on the dictionary object storing the name of the countries and their respective capitals that are searched and updated various elements from the dictionary object respectively.

So, this article explains the Dictionary Data Structure and what are the basic functions or operations that we can perform on a Dictionary Data Structure object. We also understood the usage of the Dictionary Data Structure in various programming languages like Java, Python, and C++ along with their functionalities that are required to perform the basic operations on this Data Structure. Other than these examples, there are various scenarios where we can use the Dictionary Data Structure. The most ideal scenario for using the Dictionary Data Structure where we need to store our data in key-value pairs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA