title: Getting error when trying to groupby list of dictionaries python 3.5
body: <p>I'm trying to accomplish something similar to <a href="https://stackoverflow.com/questions/34388842/python-combining-list-of-dictionaries-by-specific-key?noredirect=1&amp;lq=1">this</a> question. But I'm getting key error because not all of the lists are created equally.. I'm not sure what the best course of action would be for my case.</p>
  
  <p>My list of dicts are dynamic and I'm trying to match up key/value of 'id'.</p>
  
  <p>An example list is: </p>
  
  <pre><code>    data_keys: [[{
      'pre_conditions': [],
      'id': 436,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press*'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      }],
      'title': "Route from 'Start' to 'GetCallerDetail'"
  }],
  [{
      'pre_conditions': [],
      'id': 565,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press4025554244'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'apn_match: ',
          'content': 'press4025554244'
      }],
      'title': "Route from 'Greeting' to 'apn_match'"
  }],
  [{
      'pre_conditions': [],
      'id': 571,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press4025554244'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'apn_match: ',
          'content': 'press4025554244'
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
          press2.',
          'content': ''
      }],
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension'"
  }],
  [{
      'pre_conditions': [],
      'id': 571,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press4025554244'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'apn_match: ',
          'content': 'press4025554244'
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
          press2.',
          'content': ''
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: Again;ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
          press2.',
          'content': 'wait'
      }],
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Input &amp; Recover"
  }],
  [{
      'pre_conditions': [],
      'id': 571,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press4025554244'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'apn_match: ',
          'content': 'press4025554244'
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
          press2.',
          'content': ''
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtensionNM1: Again;ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
          press2.',
          'content': 'press8'
      }],
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Match &amp; Recover"
  }],
  [{
      'tcs_cannot_route': 'Thisedgecannotberouted,
      OnFailGoToempty',
      'id': 571,
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Input 3 Times Fail"
  }],
  [{
      'tcs_cannot_route': 'Thisedgecannotberouted,
      OnFailGoToempty',
      'id': 571,
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Match 3 Times Fail"
  }],
  [{
      'tcs_cannot_route': 'Thisedgecannotberouted,
      OnFailGoToempty',
      'id': 571,
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Input or No Match 3 Times Fail"
  }],
  [{
      'pre_conditions': [],
      'id': 571,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press4025554244'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'apn_match: ',
          'content': 'press4025554244'
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
          press2.',
          'content': ''
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: ',
          'content': 'Novalidinputs'
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtensionCNI1: ',
          'content': 'wait'
      }],
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Input &amp; Recover"
  }],
  [{
      'pre_conditions': [],
      'id': 571,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press4025554244'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'apn_match: ',
          'content': 'press4025554244'
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
          press2.',
          'content': ''
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtension: ',
          'content': 'Novalidinputs'
      },
      {
          'expected': 'ConfirmStreetNameIDV~NodeExtensionCNM1: ',
          'content': 'press1'
      }],
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Match &amp; Recover"
  }],
  [{
      'tcs_cannot_route': 'Thisedgecannotberouted,
      OnFailGoToempty',
      'id': 571,
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Input 3 Times Fail"
  }],
  [{
      'tcs_cannot_route': 'Thisedgecannotberouted,
      OnFailGoToempty',
      'id': 571,
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Match 3 Times Fail"
  }],
  [{
      'tcs_cannot_route': 'Thisedgecannotberouted,
      OnFailGoToempty',
      'id': 571,
      'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Input or No Match 3 Times Fail"
  }],
  [{
      'pre_conditions': [],
      'id': [567],
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press*'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'no_apn_match: ',
          'content': ''
      },
      {
          'expected': 'AskDOB: ',
          'content': ''
      }],
      'title': "Route from 'no_apn_match' to 'AskDOB'"
  }],
  [{
      'pre_conditions': [],
      'id': 566,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press*'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'no_apn_match: ',
          'content': ''
      }],
      'title': "Route from 'Greeting' to 'no_apn_match'"
  }],
  [{
      'pre_conditions': [],
      'id': 564,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press*'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      }],
      'title': "Route from 'GetCallerDetail' to 'Greeting'"
  }],
  [{
      'pre_conditions': [],
      'id': 568,
      'tc_steps': [{
          'expected': 'Enter_APN: PleaseentertheAPN',
          'content': 'APN: 8552929487,
          HollyBrowser: ,
          '
      },
      {
          'expected': 'Enter_Test_Date: Pleaseenterthedate',
          'content': 'press4025554244'
      },
      {
          'expected': 'Enter_ANI: ',
          'content': ''
      },
      {
          'expected': 'Greeting: ',
          'content': 'press*'
      },
      {
          'expected': 'apn_match: ',
          'content': 'press4025554244'
      },
      {
          'expected': 'AskDOB: ',
          'content': ''
      }],
      'title': "Route from 'apn_match' to 'AskDOB'"
  }]]
  </code></pre>
  
  <p>I'd like to end up with only 7 (in this case) dicts in this list</p>
  
  <p>Here's my code for this specific task:</p>
  
  <pre><code>from itertools import groupby
  from operator import itemgetter
  groups = groupby(data_keys, itemgetter('id', 'title', 'pre_conditions', 'tc_steps', 'tcs_cannot_route'))
  
  edge_data = [{'id': groupinfo[0], 'title': groupinfo[1], 'pre_conditions': groupinfo[2], 'tc_steps': groupinfo[3], 'tcs_cannot_route': groupinfo[4]} for groupinfo, items in groups]
  
  print(edge_data)
  </code></pre>