如何合并具有相同列表内不同键的两个字典?

时间:2019-11-11 13:33:18

标签: python pandas

enter image description here


[
    {"id": "cef8bab0-8482-4086-bc8f-29bf229f13f6"},
    {
        "createdAt": "2018-12-18T16:09:57.098Z",
        "notes": "Candidate initial submission.",
        "createdBy": "Steven Klinger",
    },
    {
        "createdAt": "2018-12-18T23:14:09.415Z",
        "notes": "The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>",
        "createdBy": "Matt",
    },
    {
        "createdAt": "2019-01-22T16:04:46.958Z",
        "notes": "The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>",
        "createdBy": "Matt",
    },
]

我有这个清单。里面我有两个字典。我想将两者合并到列表中的单个字典中。

1 个答案:

答案 0 :(得分:2)

如果您打算将id字典合并到具有其他结构的每个字典中,则可以这样做:

id_dict = [d for d in l if 'id' in d][0]
merge = [ {**d, **id_dict} for d in l if 'id' not in d]