Home > Blockchain >  How to filter a dictionary based on the values of a list
How to filter a dictionary based on the values of a list

Time:10-27

I have a very complex and nested dictionary (originating from a JSON File). It looks like this:

samplenumber_list = [226, 240, 554, 663, 324, 665, 398]

x = {'context first ': 'the energy levels are below zero', 
'count': 4, 
'value': 
[{'id': 424, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': '0.5', 'EnteredOn': '2018-06-28T00:00:00 02:00', 'TextValue': '0.5', 'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy zero'}, 'Sample': {'sample number': 226}}, 'Type': {'Name': 'Energt zero'}, 'Unit': None}, 
{'id': 425, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': 'spot', 'EnteredOn': '2018-10-01T16:35:01.173 02:00', 'TextValue': '30', 'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy two'}, 'Sample': {'sample number': 226}}, 'Type': {'Name': 'Energy Two'}, 'Unit': None}, 
{'id': 661, 'CompletedOn': '2018-01-10T00:00:00 01:00', 'EnteredValue': '75', 'EnteredOn': '2018-01-10T00:00:00 01:00', 'TextValue': '75', 'Test': {'Specs': 'Failed', 'TestType': {'Name': "Visuality 040"}, 'Sample': {'sample number': 239}}, 'Type': {'Name': "Visuality 040"}, 'Unit': None}, 
{'id': 673, 'CompletedOn': '2018-01-10T00:00:00 01:00', 'EnteredValue': '0', 'EnteredOn': '2018-01-10T00:00:00 01:00', 'TextValue': '0', 'Test': {'Specs': 'Passed', 'TestType': {'Name': 'scanning version'}, 'Sample': {'sample number': 240}}, 'Type': {'Name': 'Unrecorded'}, 'Unit': None}], 
'context second': 'the energy levels are still below zero'}

Now I want x to be filtered if sample number is in samplenumber_list .

This is my code:

for value in x['value']['sample']['sample number']:
  if value in samplenumber_list:
    print('create the filtered dictionary')

This is my desired output

x = {'context first ': 'the energy levels are below zero', 
'count': 4, 
'value': 
[{'id': 424, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': '0.5', 'EnteredOn': '2018-06-28T00:00:00 02:00', 'TextValue': '0.5', 'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy zero'}, 'Sample': {'sample number': 226}}, 'Type': {'Name': 'Energt zero'}, 'Unit': None}, 
{'id': 425, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': 'spot', 'EnteredOn': '2018-10-01T16:35:01.173 02:00', 'TextValue': '30', 'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy two'}, 'Sample': {'sample number': 226}}, 'Type': {'Name': 'Energy Two'}, 'Unit': None}, 
{'id': 673, 'CompletedOn': '2018-01-10T00:00:00 01:00', 'EnteredValue': '0', 'EnteredOn': '2018-01-10T00:00:00 01:00', 'TextValue': '0', 'Test': {'Specs': 'Passed', 'TestType': {'Name': 'scanning version'}, 'Sample': {'sample number': 240}}, 'Type': {'Name': 'Unrecorded'}, 'Unit': None}], 
'context second': 'the energy levels are still below zero'}

CodePudding user response:

You can do this with list comprehension:

x['value'] = [i for i in x['value'] if i['Test']['Sample']['sample number'] in samplenumber_list]

CodePudding user response:

One approach is to use a list comprehension as below:

x["value"] = [xi for xi in x["value"] if xi["Test"]["Sample"]["sample number"] in sample_number_list]
print(x)

Output

{'context first ': 'the energy levels are below zero',
 'context second': 'the energy levels are still below zero',
 'count': 4,
 'value': [{'CompletedOn': '2018-06-28T00:00:00 02:00',
            'EnteredOn': '2018-06-28T00:00:00 02:00',
            'EnteredValue': '0.5',
            'Test': {'Sample': {'sample number': 226},
                     'Specs': 'Failed',
                     'TestType': {'Name': 'Energy zero'}},
            'TextValue': '0.5',
            'Type': {'Name': 'Energt zero'},
            'Unit': None,
            'id': 424},
           {'CompletedOn': '2018-06-28T00:00:00 02:00',
            'EnteredOn': '2018-10-01T16:35:01.173 02:00',
            'EnteredValue': 'spot',
            'Test': {'Sample': {'sample number': 226},
                     'Specs': 'Failed',
                     'TestType': {'Name': 'Energy two'}},
            'TextValue': '30',
            'Type': {'Name': 'Energy Two'},
            'Unit': None,
            'id': 425},
           {'CompletedOn': '2018-01-10T00:00:00 01:00',
            'EnteredOn': '2018-01-10T00:00:00 01:00',
            'EnteredValue': '0',
            'Test': {'Sample': {'sample number': 240},
                     'Specs': 'Passed',
                     'TestType': {'Name': 'scanning version'}},
            'TextValue': '0',
            'Type': {'Name': 'Unrecorded'},
            'Unit': None,
            'id': 673}]}

As an alternative, change the the code in your question to:

res = []
for xi in x["value"]:
    if xi["Test"]["Sample"]["sample number"] in sample_number_list:
        res.append(xi)
x["value"] = res

You can find more information about looping techniques in the documentation.

CodePudding user response:

This is one way

samplenumber_list = [226, 240, 554, 663, 324, 665, 398]

x = {'context first ': 'the energy levels are below zero',
     'count': 4,
     'value':
         [{'id': 424, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': '0.5',
           'EnteredOn': '2018-06-28T00:00:00 02:00', 'TextValue': '0.5',
           'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy zero'}, 'Sample': {'sample number': 226}},
           'Type': {'Name': 'Energt zero'}, 'Unit': None},
          {'id': 425, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': 'spot',
           'EnteredOn': '2018-10-01T16:35:01.173 02:00', 'TextValue': '30',
           'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy two'}, 'Sample': {'sample number': 226}},
           'Type': {'Name': 'Energy Two'}, 'Unit': None},
          {'id': 661, 'CompletedOn': '2018-01-10T00:00:00 01:00', 'EnteredValue': '75',
           'EnteredOn': '2018-01-10T00:00:00 01:00', 'TextValue': '75',
           'Test': {'Specs': 'Failed', 'TestType': {'Name': "Visuality 040"}, 'Sample': {'sample number': 239}},
           'Type': {'Name': "Visuality 040"}, 'Unit': None},
          {'id': 673, 'CompletedOn': '2018-01-10T00:00:00 01:00', 'EnteredValue': '0',
           'EnteredOn': '2018-01-10T00:00:00 01:00', 'TextValue': '0',
           'Test': {'Specs': 'Passed', 'TestType': {'Name': 'scanning version'}, 'Sample': {'sample number': 240}},
           'Type': {'Name': 'Unrecorded'}, 'Unit': None}],
     'context second': 'the energy levels are still below zero'}

for c in x["value"]:
    # print(c["Test"]["Sample"]["sample number"])
    if c["Test"]["Sample"]["sample number"] in samplenumber_list:
        print(c)

Gives

{'id': 424, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': '0.5', 'EnteredOn': '2018-06-28T00:00:00 02:00', 'TextValue': '0.5', 'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy zero'}, 'Sample': {'sample number': 226}}, 'Type': {'Name': 'Energt zero'}, 'Unit': None}
{'id': 425, 'CompletedOn': '2018-06-28T00:00:00 02:00', 'EnteredValue': 'spot', 'EnteredOn': '2018-10-01T16:35:01.173 02:00', 'TextValue': '30', 'Test': {'Specs': 'Failed', 'TestType': {'Name': 'Energy two'}, 'Sample': {'sample number': 226}}, 'Type': {'Name': 'Energy Two'}, 'Unit': None}
{'id': 673, 'CompletedOn': '2018-01-10T00:00:00 01:00', 'EnteredValue': '0', 'EnteredOn': '2018-01-10T00:00:00 01:00', 'TextValue': '0', 'Test': {'Specs': 'Passed', 'TestType': {'Name': 'scanning version'}, 'Sample': {'sample number': 240}}, 'Type': {'Name': 'Unrecorded'}, 'Unit': None}
  • Related