What is itertools.groupby in Python?
Itertools is a module in Python that enables fast, and memory-efficient iteration over iterable data structures.
The module has a number of functions that construct and return iterators. One such function is the groupby function. This function groups data from the given iterable based on the key that is computed by a function.
Syntax
zip_groupby(iterable, key=None)
The function takes two arguments: iterables and fill value.
-
iterableis the data structure over which you want to iterate. It can be of any type such as a list, tuple, or dictionary. -
keyis a function that computes the key for each element initerable.
Return value
The function returns an iterable object with consecutive keys and groups.
Example
- In the following example, we first create an
input_listthat contains some universities and the programs offered. - We create the
key_funcfunction to compute the key based on which we will group the data. grouped_datais returned by thegroupbymethod which takes in theinput_listandkey_functo group data.- We iterate over the
grouped_dataand print the key and its corresponding value.
import itertoolsinput_list = [("University", "Harvard"),("University","MIT"),("University","UC Berkley"),("Program","Computer Science"),("Program", "Marine Biology")]key_func = lambda x: x[0]grouped_data = itertools.groupby(input_list,key_func)for key,value in grouped_data:print(key,":",list(value))