Dictionary

  • A dictionary is a collection of data values. 
  • It holds a key: value pair in which we can easily access a value if the key is known. 
  • It improves the readability of your code and makes it easier to debug
  • It is fast as the access of a value through a key is a constant time operation

Dictionary representation:

C++




// C++ program to demonstrate functionality of unordered_map
#include <iostream>
#include <unordered_map>
using namespace std;
 
int main()
{
     // Declare a Dictionary
    unordered_map<string, int> my_dict;
 
    // Adding Elements to the Dictionary
        my_dict["key1"] = 1;
        my_dict["key2"] = 2;
        my_dict["key3"] = 3;
 
    // Printing the Dictionary
    for (auto key : my_dict)
        cout << "Key: " << key.first << " Value: " << key.second << endl;
 
}
 
// This code is contributed by aadityaburujwale.


Java




/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main (String[] args) {
        
          // Declare a Dictionary
        HashMap<String,Integer> my_dict = new HashMap<>();
       
         // Adding Elements to the Dictionary
        my_dict.put("key1", 1);
        my_dict.put("key2", 2);
        my_dict.put("key3", 3);
 
          // Printing the Dictionary
        for(String key:my_dict.keySet()) {
              System.out.println("Key: "+ key +", Value: " + my_dict.get(key));
            }
    }
}


Python




# Declaring and initializing a dictionary
my_dict = {
  "key1": 1,
  "key2": 2,
  "key3": 3
}
# Printing the dictionary
 
print(my_dict)


C#




using System;
using System.Collections.Generic;
public class GFG {
 
    static public void Main()
    {
     // Declare a Dictionary
        IDictionary<string, int> my_dict= new Dictionary<string, int>();
       
     // Adding Elements to the Dictionary
        my_dict.Add("key1", 1);
        my_dict.Add("key2", 2);
        my_dict.Add("key3", 3);
 
      // Printing the Dictionary
        foreach(var kvp in my_dict) {
          Console.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
    }
  }
}


Javascript




<script>
// Javascript program to demonstrate functionality of map
 
// Declare a Dictionary
var my_dict = new Map();
 
// Adding Elements to the Dictionary
my_dict.set("key1", 1);
my_dict.set("key2", 2);
my_dict.set("key3", 3);
 
// Printing the Dictionary
console.log(my_dict);
 
// This  code is contributed by Shubham Singh
</script>


Output

{'key3': 3, 'key2': 2, 'key1': 1}

Time Complexity: O(1)
Auxiliary Space: O(1)

Differences between Array and Dictionary Data Structure

Similar Reads

Arrays:

The array is a collection of the same type of elements at contiguous memory locations under the same name.  It is easier to access the element in the case of an array.  The size is the key issue in the case of an array which must be known in advance so as to store the elements in it.  Insertion and deletion operations are costly in the case of an array since the elements are stored at contiguous memory locations.  No modification is possible at the runtime after the array is created and memory wastage can also occur if the size of the array is greater than the number of elements stored in the array....

Dictionary:

...

Comparison Between Array and Dictionary:

...

Contact Us