<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def generate_events_list(generator):
 """Populate the event_list variable to be used in jinja templates"""


    if not localized_events:
        generator.context['events_list'] = sorted(events, reverse = True,
                                                  key=lambda ev: (ev.dtstart, ev.dtend))
    else:
        generator.context['events_list'] = {k: sorted(v, reverse = True,
                                                      key=lambda ev: (ev.dtstart, ev.dtend))
                                            for k, v in localized_events.items()}