Just having trouble with itertools.groupby. Given a list of dictionaries,
my_list = [
{'name': 'stock1', 'price': 200, 'shares': 100},
{'name': 'stock2', 'price': 1.2, 'shares': 1000},
{'name': 'stock3', 'price': 0.05, 'shares': 200000},
{'name': 'stock1', 'price': 200.2, 'shares': 50}]
From this list, I was hoping to create a dictionary, where the key is the name of the stock, and the value is a list of dictionaries of that stock, for example:
{'stock1': [{'name': 'stock1', 'price': 200, 'shares': 100}, {'name': 'stock1', 'price': 200.2, 'shares': 50}] }
I ran this code:
by_name = { name: list(items) for name, items in itertools.groupby(
my_list, key=lambda x: x['name'])}
, and this is the result I got:
{'stock1': [{'name': 'stock1', 'price': 200.2, 'shares': 50}],
'stock2': [{'name': 'stock2', 'price': 1.2, 'shares': 1000}],
'stock3': [{'name': 'stock3', 'price': 0.05, 'shares': 200000}]}
For stock1, I'm expecting 2 items in the list but it only has the latest entry from the original my_list. Can anyone please point out where the mistake is? Thank you!