0
{'result':{'result':[
         {
            'Company':{
               'PostAddress':None
            },
            'ExternalPartnerProperties':None,
            'Id':123456,
            'Level':'Level1',
            'Name':'Name1',
            'ParentId':456789,
            'State':'InTrial',
            'TrialExpirationTime':1435431669
         },
         {
            'Company':{
               'PostAddress':None
            },
            'ExternalPartnerProperties':None,
            'Id':575155,
            'Level':'Level2',
            'Name':'Name2',
            'ParentId':456789,
            'State':'InTrial',
            'TrialExpirationTime':1491590226
         },
         {
            'Company':{
               'PostAddress':None
            },
            'ExternalPartnerProperties':None,
            'Id':888888,
            'Level':'Level2',
            'Name':'Name3',
            'ParentId':456789,
            'State':'InProduction',
            'TrialExpirationTime':1493280310
         },

My code:

for i in partner_output['result']['result']:
    if "InProduction" in i['State']:
        del i['Company'], i['ExternalPartnerProperties'], i['Id'], i['Level'], i['Name'], i['ParentId'], i['State'], i['TrialExpirationTime']

If I do this, then I return the following result

{'result': {'result': [{
            'Company':{
               'PostAddress':None
            },
            'ExternalPartnerProperties':None,
            'Id':123456,
            'Level':'Level1',
            'Name':'Name1',
            'ParentId':456789,
            'State':'InTrial',
            'TrialExpirationTime':1435431669
         },
         {
            'Company':{
               'PostAddress':None
            },
            'ExternalPartnerProperties':None,
            'Id':575155,
            'Level':'Level2',
            'Name':'Name2',
            'ParentId':456789,
            'State':'InTrial',
            'TrialExpirationTime':1491590226
         },
         {},

but the total number of items is still 3 ... the 3rd container is just empty, but still a container. How can I delete the 3rd container all together?

I cannot use:

for i in partner_output['result']['result']:
    if "InProduction" in i['State']:
        del partner_output['result'][i]

because I get the error:

TypeError: unhashable type: 'dict'

So I don't know what to do now :-(

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ecaz
  • 35
  • 4

1 Answers1

2

You can use a list comprehension to replace the whole list, keeping the other items:

partner_output['result']['result'] = [
    i for i in partner_output['result']['result']
    if i['State'] != "InProduction"
]

Note that the test for the filter has been reversed; you want to keep all items that do not have 'State' set to InProduction. Alternatively, keep values where the state is set to InTrial:

partner_output['result']['result'] = [
    i for i in partner_output['result']['result']
    if i['State'] == "InTrial"
]

Your second attempt failed because you tried to use i, a reference to a dictionary, as a key in the outer partner_output['result'] dictionary. If you wanted to delete something from the partner_output['result']['result'] list, you'd have to use the integer index (del partner_output['result']['result'][2]), but you can't do that in a loop because that has consequences for the for loop progress across the list.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343