pythonlist

How split (chunk) a list of customers along month/year?


I need to understand how I can generate a list for book list of customers for each month and year.

In my file .txt I have list mail (like 5000) customers and need reserve booking for max 500 costumers for each month.

month= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

year = ['2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032']

l_lines = ['a@a', 'b@b'.... ]

def generate_list_email_month(l_lines):
    kk = [l_lines[i:i + 500] for i in range(0, len(l_lines), 500)]
    mail_k = []
    
    for i,x in enumerate(kk):
        list_mails = [y for y in x]
        mail_k.append(list_mails)
    
    return mail_k


month_years = [f"{y}-{x}" for y in monthfor x in year]

Now I need to generate list of 500 costumers booked for month_years like:

Jan-2024 = ['a@a', 'b@b'.... ]
Feb-2024 = ['c@c', 'd@d'.... ]

Any suggestions on how to do it?

Thanks

For now I get the same customers but for each Jan of Year like this:

['aa@aa|Jan-2024', 'aa@aa|Jan-2025', 'aa@aa|Jan-2026']

Solution

  • Your splitting logic is functional but you need to pass the month_years to your function to build a dictionary. Your first line (kk = ...) was fine but the rest of the code wasn't doing anything useful.

    Also, you probably should increment the months first, then the years ('Jan-2024' then 'Feb-2024' not 'Jan-2025')

    Here is the updated code (with groups of 5 for the demo):

    month= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    
    year = ['2024', '2025', '2026', '2027', '2028', '2029', '2030', '2031', '2032']
    
    # crafting dummy emails for the demo
    l_lines = [f'email{x+1}' for x in range(50)]
    
    month_years = [f"{m}-{y}" for y in year for m in month]
    
    def generate_list_email_month(l_lines, month_years, n=5):
        kk = [l_lines[i:i + n] for i in range(0, len(l_lines), n)]
        # make sure we passed enough month_years
        assert len(kk) <= len(month_years), 'Not enough month-years'
        # build a dictionary of month_year/lists
        return dict(zip(month_years, kk))
    
    generate_list_email_month(l_lines, month_years)
    

    Output:

    {'Jan-2024': ['email1', 'email2', 'email3', 'email4', 'email5'],
     'Feb-2024': ['email6', 'email7', 'email8', 'email9', 'email10'],
     'Mar-2024': ['email11', 'email12', 'email13', 'email14', 'email15'],
     'Apr-2024': ['email16', 'email17', 'email18', 'email19', 'email20'],
     'May-2024': ['email21', 'email22', 'email23', 'email24', 'email25'],
     'Jun-2024': ['email26', 'email27', 'email28', 'email29', 'email30'],
     'Jul-2024': ['email31', 'email32', 'email33', 'email34', 'email35'],
     'Aug-2024': ['email36', 'email37', 'email38', 'email39', 'email40'],
     'Sep-2024': ['email41', 'email42', 'email43', 'email44', 'email45'],
     'Oct-2024': ['email46', 'email47', 'email48', 'email49', 'email50']}