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.
dict
contains heterogeneous type of elements.- Typically it is used to represent a row in a table or a sheet. But we can also use
dict
to store the aggregated results based up on a key. - We use
dict
for aggregations or joins, as we need to do a look up for a given key to update or insert.list
is not efficient for lookups. - Each and every element in a
dict
contains key value pair where key is typically column name. - Here are the common
dict
operations 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.
In [1]:
order_count_by_date = {}
In [2]:
# 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
In [3]:
order_count_by_date
Out[3]:
{'2014-01-01': 1, '2014-01-02': 1, '2014-01-03': 1}
In [4]:
# Checking if element exists in dict
'2014-01-01' in order_count_by_date
Out[4]:
True
In [5]:
'2014-01-04' in order_count_by_date
Out[5]:
False
In [6]:
# Getting value for a given key
order_count_by_date['2014-01-01']
Out[6]:
1
In [7]:
order_count_by_date['2014-01-04'] # Throws KeyError exception
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [7], in <cell line: 1>() ----> 1 order_count_by_date['2014-01-04'] KeyError: '2014-01-04'
In [8]:
order_count_by_date.get('2014-01-01')
Out[8]:
1
In [9]:
order_count_by_date.get('2014-01-04') # Returns None
In [10]:
# Updating value
order_count_by_date['2014-01-01'] = 2
In [11]:
order_count_by_date
Out[11]:
{'2014-01-01': 2, '2014-01-02': 1, '2014-01-03': 1}
In [12]:
order_count_by_date['2014-01-01'] = order_count_by_date['2014-01-01'] + 1 # Incrementing the existing value for a given key
In [13]:
order_count_by_date
Out[13]:
{'2014-01-01': 3, '2014-01-02': 1, '2014-01-03': 1}
In [14]:
order_count_by_date['2014-01-01'] += 1 # Incrementing the existing value for a given key
In [15]:
order_count_by_date
Out[15]:
{'2014-01-01': 4, '2014-01-02': 1, '2014-01-03': 1}
In [16]:
order_count_by_date.update({'2014-01-02': 2}) # Alternate way to update an existing element value in dict
In [17]:
order_count_by_date
Out[17]:
{'2014-01-01': 4, '2014-01-02': 2, '2014-01-03': 1}