Home >Backend Development >Python Tutorial >How can I group data by keys in Python and return the result in a specific format, effectively handling data with repeating keys and maintaining order?
Python Grouping Data by Keys
This guide addresses the task of grouping data by specific keys in Python. We aim to achieve an efficient and ordered grouping solution for the given dataset.
Problem Statement
Consider the following dataset represented as key-value pairs:
<code class="python">input = [ ('11013331', 'KAT'), ('9085267', 'NOT'), ('5238761', 'ETH'), ('5349618', 'ETH'), ('11788544', 'NOT'), ('962142', 'ETH'), ('7795297', 'ETH'), ('7341464', 'ETH'), ('9843236', 'KAT'), ('5594916', 'ETH'), ('1550003', 'ETH') ]</code>
The goal is to group these data pairs by their corresponding keys (the second element in each tuple) and return the grouped result in the following format:
<code class="python">result = [ { 'type': 'KAT', 'items': ['11013331', '9843236'] }, { 'type': 'NOT', 'items': ['9085267', '11788544'] }, { 'type': 'ETH', 'items': ['5238761', '962142', '7795297', '7341464', '5594916', '1550003'] } ] </code>
Solution
Following are the steps to group the data efficiently:
Create a Dictionary: Use a defaultdict to store the items for each key. Initialize the dictionary with a default factory that creates an empty list for each new key.
<code class="python">from collections import defaultdict res = defaultdict(list) for v, k in input: res[k].append(v)</code>
Convert the Dictionary to the Expected Format: To generate the final result, convert the dictionary to a list of dictionaries with the desired structure.
<code class="python">result = [{'type': k, 'items': v} for k, v in res.items()]</code>
Optional Notes:
The above is the detailed content of How can I group data by keys in Python and return the result in a specific format, effectively handling data with repeating keys and maintaining order?. For more information, please follow other related articles on the PHP Chinese website!