Sets and Dictionary

Introduction

A set is a collection of unique objects while a dictionary is a JSON-like, mapping of a key to a value.

This article will dive a little deeper in the concepts of sets and dicts as compared to our introductory article on data structures in python.

Empty Set And Dictionary

An empty set is not created by using curly braces ({})syntax as the same is reserved is creating for creating an empty dictionaries along with dict(). The only one way to get an empty set is using set()

It is worth mentioning that type function can be used to check type of any variable/object in Python.

Creating Sets and Dictionaries from List

A list can be passed to set constructor to create a set of unique elements/objects in the list.

A list of list can be passed to dict constructor to create a mapping from first element to the second element.

A list of tuple can also be used to create a dictionary.

Iterating a set object

A set object can be iterated using for loops.

Set Operations

The code below describes a few basic set operations like add, remove, union, set and difference.

Iterating a dictionary object

Using dict.items() with for loop is by far best method to iterate a dictionary object.

It can be seen that dict.items() returns a list of tuple-like object. We can also use dict.keys() to get list of keys and dict.values() to get list of values.

It should also be noted that the order of values returned by these methods may not be in the same order of insertion.

Updating Dictionary

Updating dictionary by key is very easy as shown below.

Accessing An Element From Dictionary

There are two major ways to access an element from a dictionary.

If the key is known to exist, we can safely use square-bracket([]) notation to access the value. Trying to access a value corresponding to non-existing key will result in a KeyError exception.

However, if the key is not confirmed to exist in the dictionary. We have two options.

  1. Check if the key exist first and if it does, then access the object using square-bracket notation.
  2. Use get() method something like dict.get(key, default_value) to access the element. The get() method, in case of a non-existing key returns the default value specified by the programmer.

Merging Two Dicts

There are a few situations when we need to merge one dictionary with another dictionary object. We can use dict.update() method to do the same. Below is a basic information about the same.

Counting objects using dictionary

We encounter a number of situations where we have a list of object and we want to have a count of objects of each type. Dictionary can be very useful in such cases.

Alternatively, we can also use Counter from collections standard library.

Conclusions

In this article, we touched upon sets and dictionaries in Python. These data-structures are common for any important real world problem and therefore, a clear understanding of them becomes very necessary.

Author image
New Delhi
I am a passionate programmer based in New Delhi, India. I mostly work with Python. Apart from work, I find myself doing poetry and learning about various languages. I prefer chai over coffee.