Why This Code Returns "List Index out of Range" Error

I can't work out why this code is returning a "list index out of range error. Any suggestions ?

img = None
data_values = ['2', '3', '4', '5']
print("before condition: ", data_values, type(data_values), len(data_values))
try:
    if any(value in data_values for value in ('20', '42')) or (img is not None):
        print("after condition")
        form_data_values[f'{question}'] = form_data
except Exception as e:
    print(e)

here is the output of that code:

before condition:  ['2', '3', '4', '5'] <class 'list'> 4
list index out of range
2
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article