I have a list with dates and each dict in the list has another list:
list = [
{
'date': 'X',
'tickets': [
{ 'price': 100 },
{ 'price': 120 },
{ 'price': 100 },
{ 'price': 100 },
]
},
{
'date': 'Y',
'tickets': [
{ 'price': 300 },
{ 'price': 300 },
{ 'price': 100 },
{ 'price': 100 },
]
}
]
Right now I am looping through the dates with
print('Date, Number of Tickets')
print('============')
for element in list:
print(element.date + ' - ' + len(element.tickets))
which prints
Date, Number of Tickets
============
X - 4
Y - 4
but what I want it to print is
Date, Number of Tickets, Price
============
X - 3 - 100
X - 1 - 120
Y - 2 - 300
Y - 2 - 100
So I need it to group the list of tickets and loop through each group.
So it might be something like
print('Date, Number of Tickets, Price')
print('============')
for element in list:
groups = group_by(element.tickets, 'price')
for group in groups:
print(element.date + ' - ' + group.num_tickets + ' - ' + group.price)
but I don't know how to group the tickets by price. Also, if there are no tickets for the date (i.e., tickets = []), then I still need a row saying with date=?, num_tickets=0, and price=None.
prices = [ticket['price'] for ticket in element['tickets']].