I am trying to merge a list of dictionaries in order to get a particular result.
This my list of dictionaries:
data = [{'city': 'San Francisco', 'state': 'CA', 'id': 1, 'name': 'The Musical Hop'},
{'city': 'New York', 'state': 'NY', 'id': 2, 'name': 'The Dueling Pianos Bar'},
{'city': 'San Francisco', 'state': 'CA', 'id': 3, 'name': 'Park Square Live Music & Coffee'}]
This is the desired outcome:
[{
"city": "San Francisco",
"state": "CA",
"venues": [{
"id": 1,
"name": "The Musical Hop",
}, {
"id": 3,
"name": "Park Square Live Music & Coffee",
}]
}, {
"city": "New York",
"state": "NY",
"venues": [{
"id": 2,
"name": "The Dueling Pianos Bar",
}]
}]
I tried this:
import operator
import itertools
outputList = []
for i,g in itertools.groupby(d,key= operator.itemgetter("city")):
outputList.append(list(g))
Don't forget to sort your data before applying groupby:
Output: