Home > Backend Development > Python Tutorial > 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?

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?

Mary-Kate Olsen
Release: 2024-10-31 04:11:01
Original
355 people have browsed it

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?

Python Group By

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.

Using Dictionary

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>
Copy after login

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>
Copy after login

Using itertools.groupby

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>
Copy after login

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>
Copy after login

Python Version Considerations

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template