HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Faster way to group email-date pairs by date without many nested for loops?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withoutemailgrouploopswaydatefasternestedformany

Problem

Given list of pairs (email-date):

['ann_osman@mail.com', '2 august 1976'], 
['jz_neo@mail.com', '3 august 2001'], 
['keyes_signak@mail.com', '2 october 2001'], 
['cassadi_algazi@hotmail.com', '2 october 2001'], 
['karmen_algazi@aol.com', '2 august 2001']]


I need to turn it into dictionary grouped by dates (one date → many emails).

I can do it with for loops:

def emails_for_one_date():

        for each_entry in email_date_table:

                for each_date in each_entry:
                        current_date_as_key = each_date

                        for each_entry2 in email_date_table:
                                list_emails = []
                                if each_entry[1] == current_date_as_key:
                                        list_emails.append( each_entry[0])
                                date_for_emails_dict[current_date_as_key] = list_emails
        print("------------------- dict here")
        print(date_for_emails_dict)


But Python is such a powerful language, I want to know the 'pythonic' way of doing it!
I suspect it can be one line.

Solution

A good function will should accept the data as a parameter and return the resulting data structure. Call print(emails_by_date(email_date_table)) to put it all together.

Two Pythonic techniques you want to use are:

  • dict.setdefault(key[, default]), which gets rid of the need for if, as well as the requirement for the input to be grouped chronologically



  • Multiple assignment in the for-loop, which removes the ugly [0] and [1] indexing:




If the target list is a comma-separated list of targets: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

The function could be written as:

def emails_by_date(pairs):
    ret = {}
    for email, date in pairs:
        ret.setdefault(date, []).append(email)
    return ret

Code Snippets

def emails_by_date(pairs):
    ret = {}
    for email, date in pairs:
        ret.setdefault(date, []).append(email)
    return ret

Context

StackExchange Code Review Q#58615, answer score: 6

Revisions (0)

No revisions yet.