Atir
Atir

Reputation: 75

how to merge two dictionaries with different keys that are inside same list?

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",
    },
]

i have this list. inside i have two dictionaries. i want to merge both into a single dictionary inside the list.

Upvotes: 0

Views: 87

Answers (1)

Joshua Fox
Joshua Fox

Reputation: 19655

If you intend to merge the id dict into each dict with the other structure, this works:

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]

Upvotes: 2

Related Questions