Original Answer
I have assumed you need a key and all values for that key to be appended in a list. Here I have used setdefault
method of dictionary
to achieve it.
# Input
l=[{'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'},
{'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'}]
# Main code
res = {} # defined output dict
for i in l: # for loop to fetch each element(dict) inside a list
for k, v in i.items(): # to fetch key value fair of each dict
res.setdefault(k, []).append(v) # setdefault method of add key to result and created an empty list and appended value to it.
print (res) # print result
# Output
# {'createdAt': ['2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z', '2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z'], 'notes': ['Candidate initial submission.', '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>', '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>', 'Candidate initial submission.', '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>', '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': ['Steven Klinger', 'Matt', 'Matt', 'Steven Klinger', 'Matt', 'Matt']}
Modified Answer
# NOTE: "l" is individual list of the your data set.
value_for_id = "abc" # Value to be set for id
for i in l: # For each element in l - where l is your individual list
if i.get("id",None) is not None: # verify if dict with key -> "id" exist
i["id"] = value_for_id # If exist then update the value for key -> "id"
break # break and come out of the for loop
else: # if there is no break, i.e. data doesn't have dict with "id" then we will append a new dict to the list.
l.append({"id":value_for_id}) # Appending new dict to the list
print (l)
I hope this helps and counts!