Gokul Wimalanathan Gokul Wimalanathan - 10 months ago 80
Python Question

Can a Python dictionary be passed as Neo4j Literal Maps?

I am a newbie starting with Neo4j and Python. I am trying to get some data populated into Neo4j using the Python driver.

I was wondering if it is possible to pass a Python dictionary as a Literal map described in Neo4j documentation.

This would be enable me to set app node properties at once. The properties vary from node to node so code would get ugly.

An example is given below

node1 = {
'def': '"A protein transport process that contributes to protein import into the nucleus, and that results in the vectorial transfer of a cargo-carrier protein complex through the nuclear pore complex from the cytoplasmic side to the nucleoplasmic side of the nuclear envelope." [GOC:curators, ISBN:019 8506732, PMID:14570049, PMID:9126736]',
'id': 'GO:0000060',
'is_a': ['GO:0006886'],
'name': 'protein import into nucleus, translocation',
'namespace': 'biological_process',
'relationship': ['part_of GO:0006606'],
'synonym': [ '"protein import into cell nucleus, translocation" EXACT []' ]

node2 = {
'def': '"A protein complex disassembly process that contributes to protein import into the nucleus, and that results in the dissociation of the cargo protein and the carrier (such as an importin alpha/beta heterodimer) from each other and from the nuclear pore complex." [GOC:mah, PMID:14570049, PMID:9126736, PMID:9687515]',
'id': 'GO:0000061',
'is_a': ['GO:0043624'],
'name': 'protein import into nucleus, substrate release',
'namespace': 'biological_process',
'relationship': ['part_of GO:0006606'],
'is_obselete' : True

session.run allows passing a parameter into the Cypher query, but can a python dictionary can be passed into the query, and accessed as a Neo4j Literal map.

Answer Source

Passing a dict as a parameter will turn it into a Map, which behaves exactly like a Map that you declare in Cypher. I think you're drawing a non-existent distinction.

some_python_dict = {'a': 1, 'b': 2}
    statement="CREATE (x) SET x = {dict_param}",
    parameters={'dict_param': some_python_dict}

And that's exactly how you usually set up Nodes from a language driver. If you're making several, pass a parameter that's a list of dicts instead and then UNWIND that parameter to start and you'll get the best performance.