Quick Recap of Dict Operations¶
Let us recap some of the important concepts and operations related to dict. We will primarily focus on those operations which are important for aggregations and joins.
dictcontains heterogeneous type of elements.Typically it is used to represent a row in a table or a sheet. But we can also use
dictto store the aggregated results based up on a key.We use
dictfor aggregations or joins, as we need to do a look up for a given key to update or insert.listis not efficient for lookups.Each and every element in a
dictcontains key value pair where key is typically column name.Here are the common
dictoperations relevant to aggregrations and joins.Adding elements to the dict
Checking if the key exists
Getting value for a given key
Updating value if the key exists
As part of aggregations or joins, the output is typically a
dict. It means we will have multiple records in the dict in the form of key value pairs.
order_count_by_date = {}
# Adding elements in dict
order_count_by_date['2014-01-01'] = 1
order_count_by_date['2014-01-02'] = 1
order_count_by_date['2014-01-03'] = 1
order_count_by_date
{'2014-01-01': 1, '2014-01-02': 1, '2014-01-03': 1}
# Checking if element exists in dict
'2014-01-01' in order_count_by_date
True
'2014-01-04' in order_count_by_date
False
# Getting value for a given key
order_count_by_date['2014-01-01']
1
order_count_by_date['2014-01-04'] # Throws KeyError exception
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-7-7dbb9ecd49d8> in <module>
----> 1 order_count_by_date['2014-01-04'] # Throws KeyError exception
KeyError: '2014-01-04'
order_count_by_date.get('2014-01-01')
1
order_count_by_date.get('2014-01-04') # Returns None
# Updating value
order_count_by_date['2014-01-01'] = 2
order_count_by_date
{'2014-01-01': 2, '2014-01-02': 1, '2014-01-03': 1}
order_count_by_date['2014-01-01'] = order_count_by_date['2014-01-01'] + 1 # Incrementing the existing value for a given key
order_count_by_date
{'2014-01-01': 3, '2014-01-02': 1, '2014-01-03': 1}
order_count_by_date['2014-01-01'] += 1 # Incrementing the existing value for a given key
order_count_by_date
{'2014-01-01': 4, '2014-01-02': 1, '2014-01-03': 1}
order_count_by_date.update({'2014-01-02': 2}) # Alternate way to update an existing element value in dict
order_count_by_date
{'2014-01-01': 2, '2014-01-02': 2, '2014-01-03': 1}