How to check a key exists in a Record returned by cypher?

I am using Python Driver Bolt api.

 cypher = MATCH + WHERE + RETURN + LIMIT
        
        records = tx.run(cypher)

        result_set = []
        for record in records:
            tuple = [record['nname'], record['nid']]
            if 'mname' in record and 'mid' in record:
                tuple.append(record['mname'])
                tuple.append(record['mid'])
            result_set.append(tuple)

My records optionally contain fields 'mname' and 'mid' in the result set. However, the test fails when these two fields actually contain values:
if 'mname' in record and 'mid' in record:`

It's not a standard dict in Python. How to check the presence in such a case?

Hello @lingvisa :slight_smile:

This function will transform the BOLT result into list of dictionnaries:

def bolt_to_list(result):
    return [r.data() for r in result]

You can use like this:

records = bolt_to_list(tx.run(cypher))

You can use classic Python to check for keys :slight_smile:

Regards,
Cobra

Good to know. Thanks.

1 Like