How to group dictionaries based on two values?

Related searches

How to group list of dictionaries as list of unique dictionaries based on multiple key elements?

in = [{'location': 'eastus', 'sku': 'S', 'term': 'P1', 'scope': '1'},
         {'location': 'india', 'sku': 'a', 'term': 'P1', 'scope': '2'},
         {'location': 'eastus', 'sku': 'S', 'term': 'P3', 'scope': '3'},
         {'location': 'india', 'sku': 'f', 'term': 'P1', 'scope': '4'},
         {'location': 'japan', 'sku': 'a', 'term': 'P1', 'scope': '5'},
         {'location': 'india', 'sku': 'a', 'term': 'P3', 'scope': '6'}
      ]

The key elements are 'location' and 'sku'. I want to group dictionaries having same key elements into a single dict, where the rest of the keys having different value will go into a list of separate dictionaries inside that same dict.

Expected output:

out = [{'location': 'eastus', 'sku': 'S', 'new_key': [
                                                     {'term': 'P1', 'scope': '1'}, 
                                                     {'term': 'P3', 'scope': '3'}
                                                    ]},
           {'location': 'india', 'sku': 'a', 'new_key': [
                                                     {'term': 'P1', 'scope': '2'},
                                                     {'term': 'P3', 'scope': '6'}
                                                    ]},
           {'location': 'india', 'sku': 'f', 'term': 'P1', 'scope': '4'},
           {'location': 'japan', 'sku': 'a', 'term': 'P1', 'scope': '5'},
        ]

I have tried this with required modifications but do we have a better and more accurate solution?

Using itertools.groupby

Ex:

from itertools import groupby

data = [{'location': 'eastus', 'sku': 'S', 'term': 'P1', 'scope': '1'},
         {'location': 'india', 'sku': 'a', 'term': 'P1', 'scope': '2'},
         {'location': 'eastus', 'sku': 'S', 'term': 'P3', 'scope': '3'},
         {'location': 'india', 'sku': 'f', 'term': 'P1', 'scope': '4'},
         {'location': 'japan', 'sku': 'a', 'term': 'P1', 'scope': '5'},
         {'location': 'india', 'sku': 'a', 'term': 'P3', 'scope': '6'}
      ]
result = []
for k, v in groupby(sorted(data, key=lambda x: (x["location"], x["sku"])), lambda x: (x["location"], x["sku"])):
    temp = dict(zip(('location', 'sku'), k))
    sub_value = list(v)
    if len(sub_value) == 1:
        temp.update(sub_value[0])
    else:
        temp.update({'new_key': sub_value})
    result.append(temp)

print(result)

Output:

[{'location': 'eastus',
  'new_key': [{'location': 'eastus', 'scope': '1', 'sku': 'S', 'term': 'P1'},
              {'location': 'eastus', 'scope': '3', 'sku': 'S', 'term': 'P3'}],
  'sku': 'S'},
 {'location': 'india',
  'new_key': [{'location': 'india', 'scope': '2', 'sku': 'a', 'term': 'P1'},
              {'location': 'india', 'scope': '6', 'sku': 'a', 'term': 'P3'}],
  'sku': 'a'},
 {'location': 'india', 'scope': '4', 'sku': 'f', 'term': 'P1'},
 {'location': 'japan', 'scope': '5', 'sku': 'a', 'term': 'P1'}]

Group and aggregate a list of dictionaries by multiple keys, I have a list that includes dictionaries ( List[Dict, Dict, ] ) , I would like to uniqify the list based on two keys, but I want to retain the value of another key in the� Method : Using defaultdict () + loop + dict () The defaultdict can be used to initialize the group elements and loop can be used to group the values together and conversion to dictionary can be done using dict ().

I have modified the above answer and it gives me the expected output. By the way, thanks @rakesh

    from itertools import groupby
    result = []
    keys = ('location', 'sku')
    for k, v in groupby(sorted(lst, key=lambda x: (x["location"], x["sku"])), lambda x: (x["location"], x["sku"])):
        temp = dict(zip(keys, k))
        sub_value = list(v)
        if len(sub_value) == 1:
            temp.update({'new_key': sub_value[0]})
            list(map(temp['new_key'].pop, keys))
        else:
            temp.update({'new_key': sub_value})
            for i in temp['new_key']:
                list(map(i.pop, keys))
        result.append(temp)
    print(result)

Python, While performing computations over dictionary, we can come across a The defaultdict() is used to create a dictionary initialized with lists, items() gets the key -value pair 2. Yes, we can append value on the key res[v] + [i]. Python3 Length of race track based on the final distance between participants� I have a list that includes dictionaries (List[Dict, Dict, ]) , I would like to uniqify the list based on two keys, but I want to retain the value of another key in the dictionary to make sure I do not lose it by making a list in the key I want to retain. I am using Python for the code. If it is of any significance Python 3.x to be exact.

You can do that with nested list comprehensions and some nasty lambdas too:

from itertools import groupby
import pprint

pp=pprint.PrettyPrinter()

data = [{'location': 'eastus', 'sku': 'S', 'term': 'P1', 'scope': '1'},
         {'location': 'india', 'sku': 'a', 'term': 'P1', 'scope': '2'},
         {'location': 'eastus', 'sku': 'S', 'term': 'P3', 'scope': '3'},
         {'location': 'india', 'sku': 'f', 'term': 'P1', 'scope': '4'},
         {'location': 'japan', 'sku': 'a', 'term': 'P1', 'scope': '5'},
         {'location': 'india', 'sku': 'a', 'term': 'P3', 'scope': '6'}]

pp.pprint([(lambda k,g:k.update({"new_key":[(lambda i:i.pop("location") and i.pop("sku") and i)(item) for item in g]}) or k)(k,g) for k,g in groupby(sorted(data,key=lambda i:(i["location"],i["sku"])), lambda i:{"location":i["location"],"sku":i["sku"]})])

Test is here: https://ideone.com/24bjKw. However this one destroys the original data in the process (the i.pop() calls work on the original dicts).

The chaining of steps in the inner lambdas are the only tricky parts:

lambda i:i.pop("location") and i.pop("sku") and i

uses and, because pop returns what it found, so this way short-circuit evaluation can not interfere and i is going to be the actual value of the expression. (If there would be a doubt about pop() finding the keys, it could be i.pop("...",True))

lambda k,g:k.update(...) or k

needs an or because update() returns None (and thus an and would short-circuit here).

The rest is just typing. Unfortunately I have no good idea about formatting it into a nicer shape.

Python, Sometimes, while working with data, we can be encountered with a situation in which we have list of list and we need to group it's 2nd index� I have a very large dictionary that contains key-value pairs where the key is a string and the value a list. All lists have the same lengths. I want to split up the dataset (dictionary) in two based on the values in one of the lists. One of the lists will contain binary values (i.e. 0 or 1).

Groups of things: lists, dictionaries, and sets, 2 Lists, dictionaries, and sets in docassemble; 3 Gathering information into lists. 3.1 Customizing the way The sorted() function is a built-in Python function that arranges a list in order. A “dictionary” is a group of key/value pairs. By analogy � This recipe shows two easy, efficient ways to achieve a mapping of each key to multiple values. The semantics of the two approaches differ slightly but importantly in how they deal with duplication. Each approach relies on the setdefault method of a dictionary to initialize the entry for a key in the dictionary, if needed, and in any case to

Group and Aggregate by One or More Columns in Pandas, Here's a quick example of how to group on one or multiple columns and can pass aggregation functions to the grouped object as a dictionary within the agg� We will use update where we have to match the dataframe index with the dictionary Keys. Lets use the above dataframe and update the birth_Month column with the dictionary values where key is meant to be dataframe index, So for the second index 1 it will be updated as January and for the third index i.e. 2 it will be updated as February and so on

Something like this : I have multiple dicts/key-value pairs like this: [code]d1 = {key1: x1, key2: y1} d2 = {key1: x2, key2: y2} [/code]I want the result to be a new dict (in most efficient way, if possible): [code]d = {key1: (x1, x2), key2: (y1,

Comments
  • What do you mean "better and more accurate"?
  • If your code is working, I'd suggest you post it on code review, that's the place for it.
  • It looks like you are processing a list of dictionnaries sharing same keys. Pandas does a great job with this kind of data, indexing, search, vectorized (high performance) operations on columns...
  • I don`t need all the key-value pair inside the nested list with key as 'new_key'. I just want the key-value pairs which are left out. See, my expected output @rakesh