Home > Article > Backend Development > How can I efficiently group a list of pairs in Python by the second element of each pair, while retaining the first element as a list within the grouped result?
One may encounter the need to group a set of data pairs by the second element of each pair, while retaining the first element as a list within the grouped result. This can be efficiently achieved in Python using the following steps.
Create a dictionary using the defaultdict from the collections module, where the key is the second element of the pair. Then, iterate through the input list and append the first element to the corresponding key's value.
<code class="python">import collections input = [ ('11013331', 'KAT'), ('9085267', 'NOT'), ('5238761', 'ETH'), ('5349618', 'ETH'), ('11788544', 'NOT'), ('962142', 'ETH'), ('7795297', 'ETH'), ('7341464', 'ETH'), ('9843236', 'KAT'), ('5594916', 'ETH'), ('1550003', 'ETH'), ] res = collections.defaultdict(list) for v, k in input: res[k].append(v)</code>
Convert the dictionary into the expected JSON format using a list comprehension:
<code class="python">result = [{'type': k, 'items': v} for k, v in res.items()]</code>
Another approach involves using itertools.groupby but requires the input list to be sorted by the second element.
<code class="python">from operator import itemgetter from itertools import groupby sorted_input = sorted(input, key=itemgetter(1)) groups = groupby(sorted_input, key=itemgetter(1))</code>
Create a list of dictionaries using a comprehension:
<code class="python">result = [{'type': k, 'items': [x[0] for x in v]} for k, v in groups]</code>
Before Python 3.7, the order of keys in dictionaries is not preserved. To maintain the original order, use collections.OrderedDict.
<code class="python">from collections import OrderedDict res = OrderedDict() for v, k in input: if k in res: res[k].append(v) else: res[k] = [v]</code>
Since Python 3.7, regular dictionaries maintain the order of insertion, so the OrderedDict is no longer necessary.
The above is the detailed content of How can I efficiently group a list of pairs in Python by the second element of each pair, while retaining the first element as a list within the grouped result?. For more information, please follow other related articles on the PHP Chinese website!