Jamgreen Jamgreen - 1 year ago 67
Python Question

Group list by some value in Python

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
.

Answer Source

I believe you're looking for itertools.groupby. In order for this to work, you'll need to sort the price items first.

import itertools

list_ = ...

for element in list_:
    groups = [list(g) for _, g in itertools.groupby(sorted(element['tickets'], key=lambda x: x['price']))]

    if groups:
        for group in groups:
            print(element['date'], len(group), group[0]['price'], sep=' - ')
    else:
        print(element['date'], 0, None, sep=' - ')

Output:

X - 3 - 100
X - 1 - 120
Y - 2 - 100
Y - 2 - 300

Don't name a list as list, or a dict as dict, or any other builtin name.


Now, setting list_[1]['tickets'] = []...

X - 3 - 100
X - 1 - 120
Y - 0 - None
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download