The itertools.groupby
function in Python's itertools
module returns consecutive keys and groups from the input iterable. The elements are grouped based on the value of a specified key function. It is useful for grouping elements in an iterable that share a common property.
Table of Contents
- Introduction
itertools.groupby
Function Syntax- Examples
- Basic Usage
- Grouping by Length of Words
- Grouping with a Custom Key Function
- Real-World Use Case
- Conclusion
Introduction
The itertools.groupby
function creates an iterator that returns consecutive keys and groups from the input iterable. The key function specifies a function of one argument that is used to extract the grouping key for each element. By default, it groups by the identity function, i.e., it groups consecutive identical elements.
itertools.groupby Function Syntax
Here is how you use the itertools.groupby
function:
import itertools
iterator = itertools.groupby(iterable, key=None)
Parameters:
iterable
: The input iterable whose elements are to be grouped.key
: Optional. A function that computes a key value for each element. If not specified, the identity function is used, meaning the elements are grouped by themselves.
Returns:
- An iterator that produces pairs of a key and a group iterator for consecutive keys and their groups from the input iterable.
Examples
Basic Usage
Group consecutive identical elements in a list.
Example
import itertools
data = [1, 1, 2, 2, 3, 3, 3]
grouped = itertools.groupby(data)
for key, group in grouped:
print(key, list(group))
Output:
1 [1, 1]
2 [2, 2]
3 [3, 3, 3]
Grouping by Length of Words
Group words by their length.
Example
import itertools
words = ['cat', 'dog', 'apple', 'banana', 'kiwi', 'pear']
grouped = itertools.groupby(sorted(words, key=len), key=len)
for key, group in grouped:
print(key, list(group))
Output:
3 ['cat', 'dog']
4 ['kiwi', 'pear']
5 ['apple']
6 ['banana']
Grouping with a Custom Key Function
Group numbers by their parity (even or odd).
Example
import itertools
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
grouped = itertools.groupby(data, key=lambda x: x % 2)
for key, group in grouped:
parity = 'even' if key == 0 else 'odd'
print(parity, list(group))
Output:
odd [1]
even [2]
odd [3]
even [4]
odd [5]
even [6]
odd [7]
even [8]
odd [9]
Real-World Use Case
Grouping Transactions by Date
Group transactions by their transaction date.
Example
import itertools
from datetime import datetime
transactions = [
{'date': '2023-07-21', 'amount': 100},
{'date': '2023-07-21', 'amount': 150},
{'date': '2023-07-22', 'amount': 200},
{'date': '2023-07-23', 'amount': 50},
{'date': '2023-07-23', 'amount': 75},
]
# Convert date strings to datetime objects for correct sorting and grouping
transactions.sort(key=lambda x: datetime.strptime(x['date'], '%Y-%m-%d'))
grouped = itertools.groupby(transactions, key=lambda x: x['date'])
for date, group in grouped:
print(date, list(group))
Output:
2023-07-21 [{'date': '2023-07-21', 'amount': 100}, {'date': '2023-07-21', 'amount': 150}]
2023-07-22 [{'date': '2023-07-22', 'amount': 200}]
2023-07-23 [{'date': '2023-07-23', 'amount': 50}, {'date': '2023-07-23', 'amount': 75}]
Conclusion
The itertools.groupby
function is used for grouping elements in an iterable based on a specified key function. It provides an efficient way to process and organize data that shares common properties, making it a valuable addition to your Python toolkit for data analysis and manipulation.
Comments
Post a Comment
Leave Comment