Fundamentals of Dictionaries

Fundamentals of Dictionaries

May 17, 2023
article
python, data structures, development

Are you interested in learning about a more efficient way to store and access information than using a traditional list? Consider using dictionaries. A dictionary, also known as an associative array, map, hash, or hash map, is a data structure that represents objects as key-value pairs, making it easy to access information by knowing a key.

For instance, suppose you want to create an inventory of your video games, including information like name, genre, platforms, and ratings. Instead of using a list and scanning through it to find a particular video game feature, you can define the name of the video game as a key and the rest of its information as values in a dictionary, as shown in Fig. 1. This way, you only need to look up the name of the video game (key) to access its information (values).

Dictionary Fig. 1 A sample dictionary that includes three key-value pairs from a video game collection
 

What differentiates a dictionary from a list?

Uniqueness and order. Dictionaries have keys rather than indexes and each key is unique. Generally, keys are integer or string data types. Values, on the other hand, are not unique and can be of any data type, as shown in Fig. 1. Because dictionaries lack indexes, elements in a dictionary cannot be sorted out. Therefore, when you add a new key-value pair to a dictionary, it is allocated with no intrinsic order.

What are the main operations on dictionaries?

Although the syntax and features of dictionaries can vary across programming languages, typically you can perform the following tasks:

  • Lookup a value using its key
  • Insert a new key-value pair
  • Update a value
  • Remove a key-value pair

What are some issues with dictionaries?

Dictionaries are quite efficient for lookup and insertion operations. They can get the same performance as lists by turning keys into list indexes using a hash function. However, when processing large data, dictionaries can consume a lot of resources because they are prone to allocating more memory than needed. Therefore, it is essential to consider your use case scenario before using dictionaries to store and access information.

Conclusion

Unlike traditional lists, dictionaries represent an efficient way to store and access a collection of objects using key-value pairs. If you’re interested in learning more about dictionaries, you can consult the following resources: