Python itertools.groupby() Function
The Python itertools.groupby() function is used to group consecutive elements in an iterable that have the same key. It returns an iterator that produces key-value pairs, where the key is derived from a function, and the value is an iterator containing grouped elements.
This function is commonly used for categorizing data into groups based on a certain property.
Syntax
Following is the syntax of the Python itertools.groupby() function −
itertools.groupby(iterable, key=None)
Parameters
This function accepts the following parameters −
- iterable: The iterable whose elements will be grouped.
- key (optional): A function that determines the key for each group. If not provided, the elements themselves are used as keys.
Return Value
This function returns an iterator that yields pairs of a key and an iterator of grouped elements.
Example 1
Following is an example of the Python itertools.groupby() function. Here, we group consecutive identical numbers in a list −
import itertools
numbers = [1, 1, 2, 2, 2, 3, 4, 4, 5]
for key, group in itertools.groupby(numbers):
print(key, list(group))
Following is the output of the above code −
1 [1, 1]
2 [2, 2, 2]
3 [3]
4 [4, 4]
5 [5]
Example 2
Here, we group words using the itertools.groupby() function based on their first letter −
import itertools
def first_letter(word):
return word[0]
words = ["apple", "apricot", "banana", "blueberry", "cherry", "cranberry"]
for key, group in itertools.groupby(words, key=first_letter):
print(key, list(group))
Output of the above code is as follows −
a ['apple', 'apricot']
b ['banana', 'blueberry']
c ['cherry', 'cranberry']
Example 3
Now, we use itertools.groupby() function to group a list of dictionaries based on a shared attribute −
import itertools
data = [
{"category": "fruit", "name": "apple"},
{"category": "fruit", "name": "banana"},
{"category": "vegetable", "name": "carrot"},
{"category": "vegetable", "name": "broccoli"}
]
def get_category(item):
return item["category"]
sorted_data = sorted(data, key=get_category)
for key, group in itertools.groupby(sorted_data, key=get_category):
print(key, list(group))
The result obtained is as shown below −
fruit [{'category': 'fruit', 'name': 'apple'},
{'category': 'fruit', 'name': 'banana'}]
vegetable [{'category': 'vegetable', 'name': 'carrot'},
{'category': 'vegetable', 'name': 'broccoli'}]
Example 4
We can also use the itertools.groupby() function to group numbers as even or odd −
import itertools
def is_even(n):
return "Even" if n % 2 == 0 else "Odd"
numbers = [1, 2, 2, 3, 4, 5, 6, 6, 7, 8]
for key, group in itertools.groupby(numbers, key=is_even):
print(key, list(group))
The result produced is as follows −
Odd [1]
Even [2, 2]
Odd [3]
Even [4]
Odd [5]
Even [6, 6]
Odd [7]
Even [8]
python_modules.htm