avatarKhuyen Tran

Summary

The article discusses specialized dictionary implementations in Python, such as OrderedDict, Defaultdict, ChainMap, and MappingProxyType, which provide additional functionalities like maintaining order, supplying default values, grouping multiple dictionaries, and creating read-only dictionaries.

Abstract

The article "Boost Your Efficiency With Specialized Dictionary Implementations in Python" by Khuyen Tran delves into the Python standard library's collection types that extend the capabilities of the basic dictionary data structure. It explains how OrderedDict maintains the insertion order of keys, which is crucial for certain use cases like task management. The Defaultdict is introduced for its ability to return a default value for missing keys, simplifying the process of initializing keys with common values. ChainMap is presented as a solution for grouping multiple dictionaries into a single mapping, which is useful for managing composite data sources. Lastly, MappingProxyType is highlighted for its role in creating read-only dictionaries, preventing users from altering the dictionary data. The author emphasizes the importance of understanding when to use these specialized dictionaries to enhance productivity and code efficiency, while also providing practical examples and linking to a GitHub repository for further exploration.

Opinions

  • The author believes that knowing when to use specialized dictionary implementations can significantly improve coding efficiency and productivity.
  • It is suggested that while the standard dictionary is sufficient for most cases, these specialized implementations are valuable for specific scenarios.
  • The article conveys that OrderedDict is particularly useful when the order of items is significant.
  • The use of Defaultdict is encouraged for situations where default values are frequently needed, saving time and effort.
  • ChainMap is recommended for managing data from multiple sources without merging them, maintaining a clear separation.
  • MappingProxyType is endorsed for creating a read-only view of a dictionary, which is especially useful for protecting data integrity in shared environments.
  • The author advocates for bookmarking the article and revisiting it when needing to implement these specialized dictionaries, indicating a belief in its long-term reference value.
  • The author expresses enthusiasm for data science and encourages readers to connect on LinkedIn and Twitter for further engagement on the topic.

Boost Your Efficiency With Specialized Dictionary Implementations in Python

Create dictionaries with ordered and read-only items, return default values for non-existent keys, and much more

Photo by oxana v on Unsplash

Motivation

You may be familiar with a dictionary in Python that allows for the efficient lookup, insertion, and deletion of any value associated with a given key. To put it simply, imagine going to a grocery store, if you want to find the price of an item, you just need to input the name of the item and the price associated with the item will be immediately returned.

food = {
    'fish': 10,
    'broccoli': 3,
    'tofu': 5,
    'mushroom': 3
}
food['fish']

Outcome: 10

But what if you want your dictionary to behave in a particular way that would take some thought to implement? Let’s look at four different scenarios where you want your dictionary to have a specialized implementation:

  • Creating an ordered dictionary.
  • Returning the default values when there’s no requested key.
  • Grouping multiple dictionaries into a single mapping.
  • Creating a read-only dictionary.

Create an Ordered Dictionary

Scenario

You want to use a dictionary to organize the tasks that you want to finish for the weekend. The dictionary keys are the tasks, the values are the hours needed to complete the task. Since you want to finish the tasks in the order of input — you don’t want the dictionary to mix up the order of your tasks — you decide to use OrderedDict.

OrderDict

collections.OrderedDict is ideal if key order is important for your goal.

import collections
tasks = collections.OrderedDict(laundry=0.5, shopping=2, clean=2)
tasks['movie'] = 2
tasks
tasks.keys

Outcome:

OrderedDict([('laundry', 0.5), ('shopping', 2), ('clean', 2), ('movie', 2)])
odict_keys(['laundry', 'shopping', 'clean', 'movie'])

As we can see, the items in the list are listed in the order of input. Now you just need to follow your dictionary to finish your weekend tasks in the right order.

Return the Default Value when There’s no Requested Key

Scenario

You want to create a dictionary that maps the classes to the room number. Since many classes are taught outside (because the weather is nice), you don’t want to take the time to map those classes to the Outside value. You decide to use Defaultdict.

Defaultdict

collections.Defaultdict can be initialized with a function that takes no arguments and provides the default value if a requested key cannot be found.

from collections import defaultdict
classes = defaultdict(lambda: 'Outside')
classes['Math'] = 'B23'
classes['Physics'] = 'D24'
classes['Math']

Outcome: B23. Defaultfict behaves just like a standard dictionary, except it allows the value of a non-existent key to return.

classes['English']

Outcome: Outside.

The default value that we specified is returned when the non-existence key is provided. Now you don’t need to take the time to map 20 classes that are taught outside to Outside values.

Group Multiple Dictionaries into a Single Mapping

Scenario

You want to take note of the lists of your friends and their information. Each friend’s information will be represented as a dictionary with name and age. Once you have the information on all of your friends, you contemplate how to put all that information together into a single place. You discover ChainMap.

ChainMap

collections.ChainMap behaves like its name. The data structure allows you to group multiple dictionaries together like a chain.

from collections import ChainMap
#Create multiple dictionaries of friends and their information
friend1 = {'name':'Ben','age':23}
friend2 = {'name':'Thinh', 'age': 25}
#Group these dictionaries together
friends = ChainMap(friend1, friend2)
friends

Outcome:

ChainMap({'name': 'Ben', 'age': 23}, {'name': 'Thinh', 'age': 25})

So how does the dictionary search for a provided key? It will search for the key from left to right and return the value once the key is found.

friends['name']

Outcome: Ben

Create a Read-Only Dictionary

Scenario

As you know, the dictionary allows the users to access and change the items within the dictionary. What if you just want to use the dictionary to provide the users with information about the key without changing it? For example, creating a map of classes that could not be changed by students. Then you should consider usingMappingProxyType .

MappingProxyType

MappingProxyType provides a read-only view into the wrapped dictionary’s data. This discourages users from editing the dictionary.

from types import MappingProxyType
#Can read and edit
classes = {'Math': 'B23','Physics':'D24'}
classes['Math']
classes["Math"] = 'B21'
#Can read
classes['Math']
#But can no longer edit
classes["Math"] = "D15"

Outcome:

TypeError: 'mappingproxy' object does not support item assignment

As you can see, now the students can no longer change the room to confuse their classmates.

Conclusion

Congratulations! If you have gone to the end of this article, you know what other dictionary implementations are available and when to use them. Having this knowledge will be useful as you want your dictionary to perform specific functions.

So, should you switch the standard dictionary with these special dictionary implementations? I advise you to use the standard dictionary and grab these tools if you need a specific function. Usually, you’ll find you do just fine with the standard dictionary. Save this article and go back to this as you need to implement the dictionary differently. Feel free to fork and play with the code for this article in this Github repo.

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:

Programming
Tech
Python Programming
Python
Dictionary
Recommended from ReadMedium