Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

why this code returns "list index out of range" error

Writer Matthew Barrera

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 Related questions 5273 How do I make a flat list out of a list of lists? 4357 How to find the index for a given item in a list? 1707 ?">Why not inherit from List<T>? Related questions 5273 How do I make a flat list out of a list of lists? 4357 How to find the index for a given item in a list? 1707 ?">Why not inherit from List<T>? 2987 Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3? 2245 How to remove an element from a list by index 2084 Why is it string.join(list) instead of list.join(string)? 556 Flatten an irregular (arbitrarily nested) list of lists Load 4 more related questions Show fewer related questions Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.