I have the following for loop:
def validate(request, fields=None):
i = 0
j = len(fields)
return_fields = []
for f in fields:
i += 1
if request.data[f] != check_validity(f)
if i == j:
return_fields.append(f)
return return_fields
else:
return_fields.append(f)
This checks the validity of the fields by some function check_validity(). If it is invalid, it appends it to the return_fields list and at the end, returns it to the main caller. My question is, in my code, I am using two variables i and j to just check if there are any elements left in the list 'fields'. But I am just wondering if there is a better way to do the same operation since I want to return all the invalid fields and not stop when I encounter any 1 of them.