Search code examples
neo4jpy2neo

Using python list as node properties in py2neo


I have a list of urls:

urls = ['http://url1', 'http://url2', 'http://url3']

Mind you the list can have any number of entries including 0 (none). I want to create new node property for each url (list entry). Example how the node will look like

(label{name='something', url1='http://url1', url2='http://url2'}, etc...)

It is possible to expand a dictionary with ** with the same effect I need but is there any way to do this with a list?


Solution

  • You can put your list in a dictionary and use this to create a node:

    from py2neo import Node
    
    urls = ['http://1', 'http://2']
    
    props = {}
    
    for i, url in enumerate(urls):
        # get a key like 'url1' 
        prop_key = 'url' + str(i)               
        props[prop_key] = url
    
    my_node = Node('Person', **props)
    
    graph.create(my_node)