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?

+3


source to share


3 answers


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.

+5


source


Ok, why not use a simple loop for

?

grouped_results = {}
for key, value in query_results:
    grouped_results.setdefault(key, []).append(value)

      



Output:

{'foo': ['bar', 'qux'], 'baz': ['foo']}

      

+3


source


How about using it defaultdict

?

d = defaultdict(list)
for pair in query_results:
    d[pair[0]].append(pair[1])

      

+1


source







All Articles