Itertools is a module in Python, which is always used to sort the items with the same key, and used for grouping to avoid unexpected results. itertools.groupby() used to group consecutive elements of the same value in an object such as a list. Consider the list of iterable groups that is based on a key. That key specifies what action to apply to each iterable, the result of which is then used as the heading for each grouping of the items.
The return value like as {key : value}.It is similar to a dictionary
Here, this documentation discusses,
a) Syntax
b) Parameters of itertools.groupby()
c) How to use itertools.groupby()
Syntax
itertools.groupby(iterable, key=None, or some func)
Using the same key itertools. adjacent items are collected together by itertools.groupby. It helps to sort if put differently and you can save your form from any dissimilarity.
Groupby method groups similar types. We can group similar types of objects into an iterator by using the group by method object into an iterator object. Two inputs take the function groupby() they are:-
Iterable (list, dictionary)
Key (optional)
We can make the default to the identity function key whether the key is not specified. Similar time the output of the function contains keys and groups from the iterable.
Parameters of itertools.groupby()
1. Iterable: The objects which generate an iterator are called Iterables. For example, list, tuple, string, and dictionaries are the common python iterable. You can make group elements of the iterable together with the help of itertools.groupby
2. Keyfunc: Each element's group category can be computed using keyfunc function. If not specified or none of the key functions we can use the element itself for grouping.
How to use itertools.groupby()
In Odoo, we can use itertools.grouby() ,by
From Import itertools.groupby()
Here is an example of groupby
for rec in self.order_line:
a = []
a.append(rec.id)
a.append((rec.product_id.id))
a.append((rec.product_qty))
groups.append(tuple(a))
for product in groupby(groups, key=lambda x: x[1]):
print(product)
Let’s go through the above code:
In the above code, we have the list ‘a’.
Append the tuple of the list into a group.
After passing the group to the itertools.groupby(), we get an iterator,
Here, the id of order_line, product_id, and product quantity is appended to a list called ‘a’ and the key is product_id represented in the x[1]. Using groupby we can get the final result like,
(2, <itertools._grouper object at 0x7f6ea94669a0>)
(1, <itertools._grouper object at 0x7f6ea9466d60>)
(3, <itertools._grouper object at 0x7f6ea94669a0>)
Send the iterator object into a for loop and print the keys and groups in it. Finally, the resultant output is the key and groups are in the form of the object of itertools.groupby. Consider the above code output that contains the keys and groups in the iterator object. Itertools. groupby() object groups are datatypes of this output of the code. So, don't confuse between itertools object and itertools. groupby() object.
Another example of groupby is,
groups = []
locations = env['stock.quant'].search_read(
[('product_id', '=', product.product_id.p_s_id), ('quantity', '>', 0),
('location_id.usage', '=', 'internal')], limit=1)
a = []
a.append(locations[0]['location_id'][0])
a.append(locations[0]['product_id'][0])
a.append(locations[0]['quantity'] - locations[0]['reserved_quantity'])
groups.append(tuple(a))
x = groupby(groups, key=operator.itemgetter(0))
for location, product in groupby(groups, key=operator.itemgetter(0)):
Since returns of itertools.groupby an iterator, that is a stream of tuples, the return value of itertools.groupby is the iterator of keys and groups.