bbartling
bbartling

Reputation: 3494

Python dictionary key value comparison

I have data that comes through in this format:

reg_data1 = {
    'request_id': 'bc94f069-ea3c-4465-8112-3a236b6ed8ea', 
    'profile_name': '2.0b', 
    'transport_name': 'simpleHttp', 
    'transport_address': 'None', 
    'report_only': False, 
    'xml_signature': False, 
    'ven_name': 'dan_test', 
    'http_pull_model': True
}

reg_data2 = {
    'request_id': 'bc94f069-ea3c-4465-8112-3a236b6ed8ea', 
    'profile_name': '2.0b', 
    'transport_name': 'simpleHttp', 
    'transport_address': 'None', 
    'report_only': False, 
    'xml_signature': False, 
    'ven_name': 'dan_test_asdf', 
    'http_pull_model': True
}

I am trying to create a function find_ven to see if the ven_name exists in VENS if it does exist return the ven_id and regresistration_id else return False

# Future db
VENS = {
    "1": {"ven_name": "dan_test", "ven_id": "ven_id_dan_test", "registration_id": "reg_id_dan_test"},
    "2": {"ven_name": "slipstream_ven1", "ven_id": "ven_id_slipstream_ven1", "registration_id": "reg_id_slipstream_ven1"},
    "3": {"ven_name": "volttron_test", "ven_id": "ven_id_volttron_test", "registration_id": "reg_id_volttron_test"}     
}



def find_ven(data):
    print("TRYING TO LOOK UP VEN ",data['ven_name'])
    for values in VENS.values():
        #print(values['ven_name'])
        if (values['ven_name']) in data['ven_name']:
            print(f"found {values['ven_name']} in payload, looking to match {data['ven_name']}")
            #return v['ven_id'],v['registration_id']
            return True
        else:
            return False

Can someone give me a tip where I am going wrong as both reg_data1 and reg_data2 both return True but reg_data2 should return False as the reg_data2 ven_name doesnt exist in VENS. Ultimately I am trying to use the find_ven function to return ven_id & registration_id else False

Both return True:

find_ven(reg_data1)
find_ven(reg_data2)

Any tips appreciated thanks

Upvotes: 0

Views: 69

Answers (2)

eatmeimadanish
eatmeimadanish

Reputation: 3907

I made your code easier to read, your issue was using in instead of ==:

def find_ven(data):
    print("TRYING TO LOOK UP VEN ",data['ven_name'])
    ven_name = data['ven_name']
    for v in VENS.values():
        #print(values['ven_name'])
        if v.get('ven_name') == ven_name:
            print(f"found {v.get('ven_name')} in payload, looking to match {ven_name}")
            #return v['ven_id'],v['registration_id']
            return True
    return False

Upvotes: 0

Jose7198
Jose7198

Reputation: 91

What others stated is true, the problem was using in. But also you should consider moving the return false statement outside the for loop or it will never iterate through all the VENS entries.

def find_ven(data):
     print("TRYING TO LOOK UP VEN ",data['ven_name'])
     for values in VENS.values():
         #print(values['ven_name'])
         if (values['ven_name']) == data['ven_name']:
             print(f"found {values['ven_name']} in payload, looking to match {data['ven_name']}")
             #return v['ven_id'],v['registration_id']
             return True
     return False

Upvotes: 1

Related Questions