Skip to content

Commit 48461c5

Browse files
committed
callable group_by documentation
1 parent 9d5a5d6 commit 48461c5

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

docs/basics.rst

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ String difference 2
8989

9090
>>>
9191
>>> print (ddiff['values_changed']["root[4]['b']"]["diff"])
92-
---
93-
+++
92+
---
93+
+++
9494
@@ -1,5 +1,4 @@
9595
-world!
9696
-Goodbye!
@@ -172,7 +172,7 @@ Datetime
172172
Group By
173173
--------
174174

175-
group_by can be used when dealing with the list of dictionaries. It converts them from lists to a single dictionary with the key defined by group_by. The common use case is when reading data from a flat CSV, and the primary key is one of the columns in the CSV. We want to use the primary key instead of the CSV row number to group the rows. The group_by can do 2D group_by by passing a list of 2 keys.
175+
group_by can be used when dealing with the list of dictionaries. It converts them from lists to a single dictionary with the key defined by group_by. The common use case is when reading data from a flat CSV, and the primary key is one of the columns in the CSV. We want to use the primary key instead of the CSV row number to group the rows. The group_by can do 2D group_by by passing a list of 2 keys. It is also possible to have a callable group_by, which can be used to access keys in more nested data structures.
176176

177177
For example:
178178
>>> [
@@ -249,14 +249,36 @@ Now we use group_by='id':
249249
'values_changed': {"root['BB']['James']['last_name']": {'new_value': 'Brown',
250250
'old_value': 'Blue'}}}
251251

252+
Callable group_by Example:
253+
>>> from deepdiff import DeepDiff
254+
>>>
255+
>>> t1 = [
256+
... {'id': 'AA', 'demographics': {'names': {'first': 'Joe', 'middle': 'John', 'last': 'Nobody'}}},
257+
... {'id': 'BB', 'demographics': {'names': {'first': 'James', 'middle': 'Joyce', 'last': 'Blue'}}},
258+
... {'id': 'CC', 'demographics': {'names': {'first': 'Mike', 'middle': 'Mark', 'last': 'Apple'}}},
259+
... ]
260+
>>>
261+
>>> t2 = [
262+
... {'id': 'AA', 'demographics': {'names': {'first': 'Joe', 'middle': 'John', 'last': 'Nobody'}}},
263+
... {'id': 'BB', 'demographics': {'names': {'first': 'James', 'middle': 'Joyce', 'last': 'Brown'}}},
264+
... {'id': 'CC', 'demographics': {'names': {'first': 'Mike', 'middle': 'Charles', 'last': 'Apple'}}},
265+
... ]
266+
>>>
267+
>>> diff = DeepDiff(t1, t2, group_by=lambda x: x['demographics']['names']['first'])
268+
>>> pprint(diff)
269+
{'values_changed': {"root['James']['demographics']['names']['last']": {'new_value': 'Brown',
270+
'old_value': 'Blue'},
271+
"root['Mike']['demographics']['names']['middle']": {'new_value': 'Charles',
272+
'old_value': 'Mark'}}}
273+
252274
.. _group_by_sort_key_label:
253275

254276
Group By - Sort Key
255277
-------------------
256278

257279
group_by_sort_key is used to define how dictionaries are sorted if multiple ones fall under one group. When this parameter is used, group_by converts the lists of dictionaries into a dictionary of keys to lists of dictionaries. Then, group_by_sort_key is used to sort between the list.
258280

259-
For example, there are duplicate id values. If we only use group_by='id', one of the dictionaries with id of 'BB' will overwrite the other. However, if we also set group_by_sort_key='name', we keep both dictionaries with the id of 'BB'.
281+
For example, there are duplicate id values. If we only use group_by='id', one of the dictionaries with id of 'BB' will overwrite the other. However, if we also set group_by_sort_key='name', we keep both dictionaries with the id of 'BB'.
260282

261283
Example:
262284
>>> [{'id': 'AA', 'int_id': 2, 'last_name': 'Nobody', 'name': 'Joe'},

0 commit comments

Comments
 (0)