How to merge lists of dictionaries

Viewed 8318

With lists of dictionaries such as the following:

user_course_score = [
    {'course_id': 1456, 'score': 56}, 
    {'course_id': 316, 'score': 71}
]
courses = [
    {'course_id': 1456, 'name': 'History'}, 
    {'course_id': 316, 'name': 'Science'}, 
    {'course_id': 926, 'name': 'Geography'}
]

What is the best way to combine them into the following list of dictionaries:

user_course_information = [
    {'course_id': 1456, 'score': 56, 'name': 'History'}, 
    {'course_id': 316, 'score': 71, 'name': 'Science'}, 
    {'course_id': 926, 'name': 'Geography'} # Note: the student did not take this test
]

Or would it be better to store the data differently, such as:

courses = {
    '1456': 'History',
    '316': 'Science',
    '926': 'Geography'
}

Thanks for your help.

4 Answers
Related