Solutions – Overview of Python Collections – dict and set

Exercises – Dicts and Tuples

Let us go through some exercises related to dict and tuple.

Exercise 1 – Access all the values

Get the values from the below dict in the form of a list.

In [1]:
d = {
    'order_id': 1,
    'order_date': '2013-07-25',
    'order_customer_id': 100,
    'order_status': 'COMPLETE'
}
In [2]:
def get_values(d):
    l = d.values()
    return l
In [3]:
get_values(d) # Desired output: [1, '2013-07-25', 100, 'COMPLETE']
Out[3]:
dict_values([1, '2013-07-25', 100, 'COMPLETE'])
In [4]:
type(get_values(d)) # list
Out[4]:
dict_values

Exercise 2 – Get Data types of values

Continue on previous exercise and print data type of each value returned by get_values.

In [5]:
l = get_values(d)
In [6]:
# You need to improvise on it to get the data types.
for e in l: print(type(e))
<class 'int'>
<class 'str'>
<class 'int'>
<class 'str'>
  • As l is list, you will be able to use for loop to access the elements from l. Here is the desired output.
<class 'int'>
<class 'str'>
<class 'int'>
<class 'str'>

Exercise 3 – Data Type Conversion

As part of the below dict object, order_date is of type string. You need to convert the data type of order_date to date type.

In [7]:
d = {
    'order_id': 1,
    'order_date': '2013-07-25',
    'order_customer_id': 100,
    'order_status': 'COMPLETE'
}
In [8]:
import datetime

datetime.datetime.strptime(d['order_date'], '%Y-%m-%d')
Out[8]:
datetime.datetime(2013, 7, 25, 0, 0)
In [9]:
import datetime

def convert_date_type(d):
    d['order_date'] = datetime.datetime.strptime(d['order_date'], '%Y-%m-%d')
    return d
  • Run below cell to validate the output. Here is the desired output.
{'order_id': 1,
 'order_date': datetime.datetime(2013, 7, 25, 0, 0),
 'order_customer_id': 100,
 'order_status': 'COMPLETE'}
In [10]:
d_converted = convert_date_type(d)
d_converted
Out[10]:
{'order_id': 1,
 'order_date': datetime.datetime(2013, 7, 25, 0, 0),
 'order_customer_id': 100,
 'order_status': 'COMPLETE'}
In [11]:
type(d_converted['order_date']) # Output: datetime.datetime
Out[11]:
datetime.datetime

Exercise 4 – Create list of dicts

Create list of dicts for the following data.

  • Each element in the list should be of type dict.
  • The dict should contain order_item_order_id and order_item_subtotal as keys.
order_item_order_id order_item_subtotal
1 299.98
2 199.99
2 250.0
2 129.99
4 49.98
4 299.95
4 150.0
4 199.92
In [12]:
orders = [
    {'order_item_order_id': 1, 'order_item_subtotal': 299.98},
    {'order_item_order_id': 2, 'order_item_subtotal': 199.99},
    {'order_item_order_id': 2, 'order_item_subtotal': 250.0},
    {'order_item_order_id': 2, 'order_item_subtotal': 129.99},
    {'order_item_order_id': 4, 'order_item_subtotal': 49.98},
    {'order_item_order_id': 4, 'order_item_subtotal': 299.95},
    {'order_item_order_id': 4, 'order_item_subtotal': 150.0},
    {'order_item_order_id': 4, 'order_item_subtotal': 199.92}
]

Exercise 5: Dict with max value

Create a dict with order id and max order item subtotal using the below dict. The below dict contains order id as key and order item subtotals list as corresponding value.

In [13]:
order_item_subtotals = {
    1: [299.98],
    2: [199.99, 250.0, 129.99],
    4: [49.98, 150.0, 199.92],
    5: [299.98, 299.95, 99.96, 299.98, 129.99],
    7: [199.99, 299.98, 79.95],
    8: [179.97, 299.95, 199.92, 50.0]
}
In [14]:
list(order_item_subtotals.items())
Out[14]:
[(1, [299.98]),
 (2, [199.99, 250.0, 129.99]),
 (4, [49.98, 150.0, 199.92]),
 (5, [299.98, 299.95, 99.96, 299.98, 129.99]),
 (7, [199.99, 299.98, 79.95]),
 (8, [179.97, 299.95, 199.92, 50.0])]
In [15]:
item = list(order_item_subtotals.items())[2]
In [16]:
(item[0], max(item[1]))
Out[16]:
(4, 199.92)
In [17]:
# Your solution should go here
max_subtotals = []
for item in order_item_subtotals.items():
    max_subtotals.append((item[0], max(item[1])))

# make sure this is out of for loop. By now max_subtotals is list of tuples.
# But the expectation is to create a dict
dict(max_subtotals)
Out[17]:
{1: 299.98, 2: 250.0, 4: 199.92, 5: 299.98, 7: 299.98, 8: 299.95}
  • Here is the desired output.
{1: 299.98, 2: 250.0, 4: 199.92, 5: 299.98, 7: 299.98, 8: 299.95}

Exercise 6: Dict with order revenue

Create a dict with order item id and order item subtotal. The input dict will contain order item id as key and quantity along with price as value. order item subtotal is product of quantity and price.

In [18]:
order_items = {1: (1, 299.98),
 2: (1, 199.99),
 3: (5, 50.0),
 4: (1, 129.99),
 5: (2, 24.99),
 6: (5, 59.99),
 7: (3, 50.0),
 8: (4, 49.98),
 9: (1, 299.98),
 10: (5, 59.99)}
In [19]:
list(order_items.items())
Out[19]:
[(1, (1, 299.98)),
 (2, (1, 199.99)),
 (3, (5, 50.0)),
 (4, (1, 129.99)),
 (5, (2, 24.99)),
 (6, (5, 59.99)),
 (7, (3, 50.0)),
 (8, (4, 49.98)),
 (9, (1, 299.98)),
 (10, (5, 59.99))]
In [20]:
item = list(order_items.items())[0]
In [21]:
(item[0], item[1][0] * item[1][1])
Out[21]:
(1, 299.98)
In [22]:
# Your solution should go here
order_revenues = []
for item in order_items.items():
    order_revenues.append((item[0], item[1][0] * item[1][1]))

# make sure this is out of for loop. By now order_revenues is list of tuples.
# But the expectation is to create a dict
dict(order_revenues)
Out[22]:
{1: 299.98,
 2: 199.99,
 3: 250.0,
 4: 129.99,
 5: 49.98,
 6: 299.95,
 7: 150.0,
 8: 199.92,
 9: 299.98,
 10: 299.95}
  • Here is the expected output.
{1: 299.98,
 2: 199.99,
 3: 250.0,
 4: 129.99,
 5: 49.98,
 6: 299.95,
 7: 150.0,
 8: 199.92,
 9: 299.98,
 10: 299.95}
In [ ]:
 

Share this post