How to append text to an empty array in Zapier using Python

I’m facing an issue with my Python code in Zapier while trying to add a text string to an empty array. The same code works perfectly on my local setup, but it fails when I run it in Zapier.

Here’s the code I’m using:

my_list = []
if (input_data[0] == "true"): my_list.append("sample_string")

However, I receive this error message:

Traceback (most recent call last):
File "/tmp/tmpAbC456/usercode.py", line 10, in the_function
if (input_data[0] == "true"): my_list.append("sample_string")
KeyError: 0

It looks like there might be a problem with accessing the first element of my input data. Has anyone else dealt with this issue? What might be causing the difference between my local environment and what Zapier provides?

The KeyError you’re encountering suggests that input_data doesn’t have an element at index 0, which means it’s likely an empty list or dict rather than containing the data you expect. I’ve run into similar issues when working with Zapier’s Python code step. The problem usually stems from how Zapier passes data between steps. Instead of assuming the structure, try checking what you’re actually receiving first. Add a line like print(input_data) or print(type(input_data)) to see the actual format. In my experience, Zapier sometimes passes data as a dictionary rather than a list, so you might need to access it differently. Also consider adding a length check before accessing the element to avoid the error entirely.

yeah ive seen this before, zapier’s input handling is kinda weird compared to local python. try using input_data.get(0) instead or wrap it in a try/except block. also make sure your previous step is actually sending data - sometimes empty triggers cause this exact error.

This happens because Zapier handles input data differently than your local environment. The input_data variable in Zapier is typically a dictionary, not a list, even when it appears to be indexed numerically. I encountered this exact issue when migrating scripts to Zapier’s Code by Zapier step. Instead of accessing input_data[0], you should reference the actual field name from your previous step. For example, if your previous step outputs a field called “condition”, use input_data['condition'] instead. You can also add some debugging by printing the keys available with print(input_data.keys()) to see what Zapier is actually passing through. The structure depends entirely on how your previous Zapier step formats its output, which explains why your local version works but Zapier throws the KeyError.