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

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?

Linda Hamilton
Linda HamiltonOriginal
2024-10-28 14:08:30847browse

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:

  1. 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>
  2. 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:

  • Prior to Python 3.7, dictionaries did not maintain insertion order. To preserve the original order of the keys, consider using an OrderedDict instead.
  • Alternatively, the itertools.groupby function can also be employed for grouping, but it requires the input to be sorted beforehand.
  • For large datasets, using a database with a grouping function may be more efficient than the in-memory solutions presented here.

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!

Statement:
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