No, but you can use a list:
def make_nodes(n):
nodes = []
nodes.append(Node(0,None)) # head node
for i in range(1, n):
nodes.append(Node(i, None))
nodes[i-1].next = nodes[i] #somehow link them
return nodes
nodes = make_nodes()
head = nodes[0]
second = nodes[1]
last = nodes[-1]
You could also use a dictionary, and use the node number as the key. But a list seems more natural in this case.
But why would you want to do this? You might as well just use a Python list
of Node
s. Creating the node list can be easily done with a list comprehension. Iterating over the list could then be done with a simple for loop:
nodes = [Node(i) for i in range(n)]
for node in nodes:
print(node.payload)