How to create another dictionary which contains string

I am trying to extract the dictionary which contains string

dictionary is below

[ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" }, 
{ "id": "2", "name": "B", "businessArea": [ "Engineering" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
 { "id": "3", "name": "C", "businessArea": [ "Engineering" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]

I am trying to extract

  • the businessArea is Engineering and,
  • Role which is Tester or Developer and
  • Designation is L1 and
  • Location is NY

Code is below

def get_set(d,field):
    return {d[field]} if isinstance(d[field], str) else set(d[field])
    
# we use this to filter
def validate(d):
    if 'Role' in d or `businessArea` in d or `Designation` in d or `Location` in d :
        return get_set(d,'Role').intersection({'Developer','Tester'}) and \
               get_set(d,'businessArea').intersection({'Engineering'}) and \
               get_set(d,'Designation').intersection({'L1'}) and \
               get_set(d,'Location').intersection({'NY'})

result = [d for d in test if validate(d)]
  • I am getting null list

Expected out is [{ "id": "3", "name": "C", "businessArea": [ "Engineerring" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]

Adding one more dictionary to test

[{ 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Accounting' }, { 'id': '3', 'name': 'Accounting' } ],'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '2', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Accounting' } ], 'Role': [ { 'id': '5032', 'name': 'Tester' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Accounting' } ], 'Role': [ { 'id': '5032', 'name': 'Developer' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }] }]

second dictionary to test

[{'_index': '1',
  '_type': '_doc',
  '_id': '1',
  '_score': 1.0,
  '_source': {'id': '1',
   'name': 'A',
   'businessArea': [{'id': '25', 'name': 'Accounting'}],
   'Role': ['Developer'],
   'Designation': ['L2'],
   'Location': 'NY'}},
 {'_index': '1',
  '_type': '_doc',
  '_id': '2',
  '_score': 1.0,
  '_source': {'id': '2',
   'name': 'B',
   'businessArea': [{'id': '25', 'name': 'Engineering'}],
   'Role': ['Tester', 'Developer'],
   'Designation': ['L1'],
   'Location': 'NY'}},
 {'_index': '1',
  '_type': '_doc',
  '_id': '3',
  '_score': 1.0,
  '_source': {'id': '3',
   'name': 'C',
   'businessArea': [{'id': '25', 'name': 'Engineering'}],
   'Role': ['Tester', 'Developer'],
   'Designation': ['L1'],
   'Location': 'NY'}}]

3 answers

  • answered 2020-10-16 04:31 Wasif Hasan

    Try like this:

    myList = [ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" }, 
    { "id": "2", "name": "B", "businessArea": [ "Engineerring" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
     { "id": "3", "name": "C", "businessArea": [ "Engineerring" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]
    for x in myList:
      if (("Tester" in x['Role'] or "Developer" in x['Role']) and (x["Designation"] == "L1")) and (x["Location"] == "NY"): 
        print(x)
        
        
    

  • answered 2020-10-16 05:03 Vinicius

    You have a typo on your examples (engineerring):

    [ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" }, 
    { "id": "2", "name": "B", "businessArea": [ "Engineering" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
     { "id": "3", "name": "C", "businessArea": [ "Engineering" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]
    

  • answered 2020-10-16 05:35 DarrylG

    Fix of Original Code

    You can get your code to run by replacing or with and in if statement in function validate

    def get_set(d,field):
        return {d[field]} if isinstance(d[field], str) else set(d[field])
        
    # we use this to filter
    def validate(d):
        # change or to and in if statement
        if 'Role' in d and 'businessArea' in d and 'Designation' in d and 'Location' in d :
            return get_set(d,'Role').intersection({'Developer','Tester'}) and \
                   get_set(d,'businessArea').intersection({'Engineering'}) and \
                   get_set(d,'Designation').intersection({'L1'}) and \
                   get_set(d,'Location').intersection({'NY'})
    
    test = [ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" }, 
    { "id": "2", "name": "B", "businessArea": [ "Engineering" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
     { "id": "3", "name": "C", "businessArea": [ "Engineering" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]
    
    result = [d for d in test if validate(d)]   
    
    print(result)
    

    Output

    [{'id': '3',
      'name': 'C',
      'businessArea': ['Engineering'],
      'Role': ['Developer'],
      'Designation': ['L1'],
      'Location': 'NY'}]
    

    New Algorithm to Handle Nested Dictionaries

    def contains(item, field, values):
        '''
           Search through nested dictionary starting a key field
           to find if a value is in values
        '''
        if field:
            item = item.get(field)      # Get value of field
        if item is None:
            return None                 # Done if field not found
        if isinstance(item, list):
            return any(contains(v, None, values) for v in item)  # recursively check if any value in list in values
        if isinstance(item, dict):
            return any(contains(v, None, values) for v in item.values())  # recursively check if any value in dictionary in values
        return item in values       # Not list or dict, so check if item in values
        
    def validate(d):
        return contains(d, 'Role', {'Developer','Tester'}) and \
               contains(d, 'businessArea', {'Engineering'}) and \
               contains(d, 'Designation', {'L1'}) and \
               contains(d, 'Location', {'NY'})
    

    Tests

    Test 1

    test = [ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" }, 
    { "id": "2", "name": "B", "businessArea": [ "Engineering" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
     { "id": "3", "name": "C", "businessArea": [ "Engineering" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]
    

    Output 1

    [{'id': '3', 'name': 'C', 'businessArea': ['Engineering'], 'Role': ['Developer'], 'Designation': ['L1'], 'Location': 'NY'}]
    

    Test 2

    test = [{'id': '1',   'name': 'A',   'businessArea':[{'id': '25', 'name': 'Accounting'}],   'Role': ['Developer'],   'Designation': ['L2'],   'Location': 'NY'},  {'id': '2',   'name': 'B',   'businessArea':[{'id': '25', 'name': 'Engineering'}],   'Role': ['Tester', 'Developer'],   'Designation': ['L1'],   'Location': 'NY'},  {'id': '3',   'name': 'C',   'businessArea':[{'id': '25', 'name': 'Engineering'}],   'Role': ['Tester', 'Developer'],   'Designation': ['L1'],   'Location': 'NY'}]
    result = [d for d in t if validate(d)]  
    print(result)
    

    Output 2

    [{'id': '2', 'name': 'B', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}, {'id': '3', 'name': 'C', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}]
    

    Test 3

    test = [{ 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Accounting' }, { 'id': '3', 'name': 'Accounting' } ],'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '2', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Accounting' } ], 'Role': [ { 'id': '5032', 'name': 'Tester' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Accounting' } ], 'Role': [ { 'id': '5032', 'name': 'Developer' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }] }]
    result = [d for d in t if validate(d)] 
    print(result)
    

    Output 3

    [{'id': '2', 'name': 'B', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}, {'id': '3', 'name': 'C', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}]
    

    Test 4 (2nd dictionary in question)

    test = [{'_index': '1',
      '_type': '_doc',
      '_id': '1',
      '_score': 1.0,
      '_source': {'id': '1',
       'name': 'A',
       'businessArea': [{'id': '25', 'name': 'Accounting'}],
       'Role': ['Developer'],
       'Designation': ['L2'],
       'Location': 'NY'}},
     {'_index': '1',
      '_type': '_doc',
      '_id': '2',
      '_score': 1.0,
      '_source': {'id': '2',
       'name': 'B',
       'businessArea': [{'id': '25', 'name': 'Engineering'}],
       'Role': ['Tester', 'Developer'],
       'Designation': ['L1'],
       'Location': 'NY'}},
     {'_index': '1',
      '_type': '_doc',
      '_id': '3',
      '_score': 1.0,
      '_source': {'id': '3',
       'name': 'C',
       'businessArea': [{'id': '25', 'name': 'Engineering'}],
       'Role': ['Tester', 'Developer'],
       'Designation': ['L1'],
       'Location': 'NY'}}]
    
    result = [d for d in t if validate(d)] 
    print(result)
    

    Output

    [{'id': '2', 'name': 'B', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}, {'id': '3', 'name': 'C', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}]