Naomi See
Naomi See

Reputation: 101

Getting error when trying to groupby list of dictionaries python 3.5

I'm trying to accomplish something similar to this 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.

My list of dicts are dynamic and I'm trying to match up key/value of 'id'.

An example list is:

    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 & 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 & 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 & 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 & 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'"
}]]

I'd like to end up with only 7 (in this case) dicts in this list

Here's my code for this specific task:

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)

Upvotes: 2

Views: 33

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53535

Two issues spotted in the code above:

  1. You're using "tc_step" in itemgetter while the key is "tc_steps"
  2. You're using "tcs_cannot_route" while this key doesn't exist in some records

There might be other issues as well.

Upvotes: 1

Related Questions