How to merge lists of dictionaries

后端 未结 4 1922
情歌与酒
情歌与酒 2020-12-03 11:49

With lists of dictionaries such as the following:

user_course_score = [
    {\'course_id\': 1456, \'score\': 56}, 
    {\'course_id\': 316, \'score\': 71}
]
         


        
4条回答
  •  余生分开走
    2020-12-03 12:02

    dictionary is basically a list of (key, value) pairs.

    in your case,

    user_course_score can just be a dictionary of (course_id, score) rather than a list of dictionaries (you are just complicating it unnecessarily)

    similarly, course can just be a dictionary of (course_id, name)

    what you have suggested in the end is the right way :)

提交回复
热议问题