pythonjsongroup-bymergeddictionaries

Merging dictionaries and keeping unique values


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))

Solution

  • Don't forget to sort your data before applying groupby:

    import itertools
    import operator
    
    result = []
    
    for (city, state), group in itertools.groupby(
            sorted(data, key=operator.itemgetter('city')),
            key=operator.itemgetter('city', 'state')
    ):
        venues = [{'id': obj['id'], 'name': obj['name']} for obj in group]
        result.append(dict(
            city=city,
            state=state,
            venues=venues
        ))
    
    print(result)
    

    Output:

    [{'city': 'New York',
      'state': 'NY',
      'venues': [{'id': 2, 'name': 'The Dueling Pianos Bar'}]},
     {'city': 'San Francisco',
      'state': 'CA',
      'venues': [
          {'id': 1, 'name': 'The Musical Hop'},
          {'id': 3, 'name': 'Park Square Live Music & Coffee'}]
      }]