Unveiling the Magic of Python Dictionaries and Sets

Published On Mon Jul 22 2024
Unveiling the Magic of Python Dictionaries and Sets

Engaging with Dictionaries and Sets in Python - Open Source For You

In this first article in a two-part series, we will delve into the fascinating world of dictionaries in Python and uncover some insights that are sure to captivate your interest.

In Python, dictionaries and sets are used to save unique, unordered, mutable (also immutable — frozen sets) and quick retrieval elements. In any Python program, there are a number of dicts used even when the program is not using these explicitly; for example, CPython uses dictionary lookup for any attribute, class, or global variable and more. As this plays an important role, it has been highly optimized by Python’s core developers, especially for search, add, and delete operations. Hash tables are the key element behind the implementation of dicts and sets, making these data structures efficient. These tables are powerful and are also used in solving problems like indexing of database tables, caching, name lookups, and so on.

Working with Dictionaries

A dictionary is composed of a series of key-value mapping elements. The keys and values can be of mixed types like string and integer. Here are a few methods to create a dict:

  • The update() method inserts the specified items to the dictionary. These items can be a dictionary or an iterable object with key-value pairs.
  • The pop() method removes the specified item from the dictionary.
  • The __missing__ method is called by the __getitem__() dictionary method internally if the keys don’t exist.

Here’s an example:

View Objects Methods

The items() method returns a view object. The view object contains the key-value pairs of the dictionary as tuples in a list and will reflect any changes done to the dictionary. The keys() method returns a view object of all the available first level keys in the dictionary. The values() method returns a view object in Python that contains the values of the dictionary as a list.

We will just explore some sample examples here, but a very good blog at https://learnpython.com/blog/filter-dictionary-in-python/ details many more possibilities.

Creating and Updating Dictionaries

The fromkeys() method returns a dictionary with the specified keys and the specified value. The setdefault() method returns the value of a key if the key is in the dictionary.

Python Dictionary Comprehension Tutorial – Master Data Skills + AI

A defaultdict is configured to create items on demand whenever a missing key is searched. You can transform a dictionary from one form to another using dict comprehensions, which make the code easier to read, avoid loops, and enhance performance. You can log on to https://www.datacamp.com/tutorial/python-dictionary-comprehension for multiple such examples.

Ordered Dictionaries

Ordered dictionaries are designed to remember the order of items. The move_to_end method is used to move an existing key of the dictionary either to the end or to the beginning. The reversed method reverses the order of the dict keys stored. ChainMap holds the list of dictionaries, where key search is performed based on the order of the dictionaries stored.

A MappingProxyType builds a read-only dictionary instance. You can find further details at https://lerner.co.il/2019/05/12/python-dicts-and-memory-usage/.

Performance Analysis

The fastest way to repeatedly lookup data with millions of entries in Python is by using dictionaries. In general, dictionaries offer far better performance compared to lists and tuples, especially when it comes to search, add, and delete operations. Sets are similar to dictionaries, but they lack key-value pairing and consist of unique, unordered elements.

For more details, you can refer to the following link: Stack Overflow - Dictionary Comprehensions.