How to Retrieve Data from Previous Nodes in Python within n8n (similar to $items("NodeName") in JS)?

Hello everyone,

I’ve been using n8n to automate various tasks, and I have a question regarding fetching data from previous nodes when I’m working with the “Execute Python Script” node.

In JavaScript, within the “Function” nodes, it’s straightforward to directly fetch data from a specific node using the $items("NodeName") syntax. However, I haven’t found an equivalent method when I’m scripting in Python.

Does anyone know how, within a Python script in n8n, I can fetch data from a specific node (by its name) in the same way I do in JS?

Any insights or suggestions would be greatly appreciated.

Thanks in advance!

Very sorry for that. Did not realized that our docs actually do not provide any useful information or examples on how to use the node! Already created a ticket to get that fixed asap.

Generally can you use all the built-in variables also in Python. The difference is that Python does not allow the usage of $ in variable names. For that reason do they start with an underscore(_). Meaning if you write in JavaScript $input.first().json.name you have to write _input.first().json.name in Python instead.

In JavaScript you would normally access data in the new syntax with $("NodeName"). You can find the JavaScript-Docs here. Meaning in the Python it would be _("NodeName"). So if you want to get for example all items it would be _("NodeName").all().

I hope that is helpful!

4 Likes

Thank you so much for your guidance. Your explanation was spot-on and helped me resolve my issue. I appreciate the prompt and detailed response. Have a great day

Glad to hear! Have fun!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.