'Intersect programs and enrollments.
Builds a dictionary of program dict lists keyed by course run ID. The
resulting dictionary is suitable in applications where programs must be
filtered by the course runs they contain (e.g., the student dashboard).
Returns:
defaultdict, programs keyed by course run ID'

def invert_programs(self):

    inverted_programs = defaultdict(list)
    for program in self.programs:
        for course in program['courses']:
            for course_run in course['course_runs']:
                course_run_id = course_run['key']
                if (course_run_id in self.course_run_ids):
                    program_list = inverted_programs[course_run_id]
                    if (program not in program_list):
                        program_list.append(program)
    for program_list in inverted_programs.itervalues():
        program_list.sort(key=(lambda p: p['title']))
    return inverted_programs
