excellentit has "sentour -mail to the two expected addresses with subject and message contents included now that we can send messageslet' work on the -mail group management system we'll need an object that somehow matches -mail addresses with the groups they are in since this is many-to-many relationship (any one -mail address can be in multiple groupsany one group can be associated with multiple -mail addresses)none of the data structures we've studied seems quite ideal we could try dictionary of group-names matched to list of associated -mail addressesbut that would duplicate -mail addresses we could also try dictionary of -mail addresses matched to groupsresulting in duplication of groups neither seems optimal let' try this latter versioneven though intuition tells me the groups to -mail address solution would be more straightforward since the values in our dictionary will always be collections of unique -mail addresseswe should probably store them in set container we can use defaultdict to ensure that there is always set container available for each keyfrom collections import defaultdict class mailinglist'''manage groups of -mail addresses for sending -mails ''def __init__(self)self email_map defaultdict(setdef add_to_group(selfemailgroup)self email_map[emailadd(groupnowlet' add method that allows us to collect all the -mail addresses in one or more groups this can be done by converting the list of groups to setdef emails_in_groups(self*groups)groups set(groupsemails set(for eg in self email_map items()if groupsemails add(ereturn emails firstlook at what we're iterating overself email_map items(this methodof coursereturns tuple of key-value pairs for each item in the dictionary the values are sets of strings representing the groups we split these into two variables named and gshort for -mail and groups we add the -mail address to the set of return values only if the passed in groups intersect with the -mail address groups the groups syntax is shortcut for intersection(groups)the set class does this by implementing the special __and__ method to call intersection