Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group emails into TO & CC with itertools.groupby and convert it to a dictionary

I’d like to group emails by their domain and convert the result into a dictionary. So far I have figured out that itertools.groupby with a custom func will do that. It correctly assigns keys to each value, but when I try to create a dictionary only the last value is used when the values to be grouped are not continues.


import re
from itertools import groupby

{k: list(v) for k, v in groupby(["bar", "foo", "baz"], key=lambda x: "to" if re.search(r"^b", x) else "cc")}

This will produce {'to': ['baz'], 'cc': ['foo']} instead of {'to': ['bar', 'baz'], 'cc': ['foo']}.

How I can fix that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use dict.setdefault OR collections.defaultdict(list) and extend in list like below.

# from collections import defaultdict
# dct = defaultdict(list)

dct = {}
for k, v in groupby(["awol", "bar", "foo", "baz"], 
                    key=lambda x: "to" if re.search(r"^b", x) else "cc"):
    dct.setdefault(k,[]).extend(list(v))
print(dct)

{'cc': ['awol', 'foo'], 'to': ['bar', 'baz']}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading