Python array of groups of tuples first, store second
So I have a set of tuples, something like this
query_results = [("foo", "bar"), ("foo", "qux"), ("baz", "foo")]
I would like to achieve something like:
{
"foo": ["bar", "qux"],
"baz": ["foo"]
}
So I tried to use this
from itertools import groupby
grouped_results = {}
for key, y in groupby(query_results, lambda x: x[0]):
grouped_results[key] = [y[1] for u in list(y)]
The problem is, although the number of keys is correct, the number of values ββin each array is significantly lower than it should be. Can anyone explain why this is happening and what should I do?
source to share
For this it is better to use defaultdict
:
from collections import defaultdict
result = defaultdict(list)
for k,v in query_results:
result[k].append(v)
What gives:
>>> result
defaultdict(<class 'list'>, {'baz': ['foo'], 'foo': ['bar', 'qux']})
If you want to re-include it in the vanilla dictionary, you can - after the loop for
use:
result = dict(result)
This leads to:
>>> dict(result)
{'baz': ['foo'], 'foo': ['bar', 'qux']}
A defaultdict
is being built with a factory, here list
. If the key is not found in the dictionary, the factory is called ( list()
creates a new empty list). The result is then associated with a key.
So, for every key k
that is not yet in the dictionary, we first build a new list. We then call .append(v)
this list to add values ββto it.
source to share