How do I use itertools.groupby()?
Python tip:
You can use
groupby
fromitertools
to group elements.
- Returns - an iterator of consecutive keys and groups from the iterable.
- Keys - values returned from
key
function- 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']