How do I use itertools.groupby()?


Python tip:

You can use groupby from itertools to group elements.

  1. Returns - an iterator of consecutive keys and groups from the iterable.
  2. Keys - values returned from key function
  3. Groups - iterator returns values for group

For example, to group tax IDs 👇

import re
from itertools import groupby

tax_ids = [
    "DE123456789",
    "ATU99999999",
    "BG999999999",
    "IT12345678900",
    "DE987654321",
]

grouped_tax_ids = groupby(
    tax_ids, key=lambda tax_id: re.match(r"[A-Z]{2}", tax_id).group()
)

for country, country_tax_ids in grouped_tax_ids:
    print(country, list(country_tax_ids))

# DE ['DE123456789']
# AT ['ATU99999999']
# BG ['BG999999999']
# IT ['IT12345678900']
# DE ['DE987654321']