def validate_lists(lists, expected_length):
"""
Validates a list of lists to ensure they all have the expected length.
Handles failures gracefully without async logic.
Args:
lists: A list of lists to validate.
expected_length: The expected length of each inner list.
Returns:
A list of validation errors. Empty list if all lists are valid.
"""
errors = []
for i, lst in enumerate(lists):
if not isinstance(lst, list):
errors.append(f"Error: Element at index {i} is not a list.")
continue
if len(lst) != expected_length:
errors.append(f"Error: List at index {i} has length {len(lst)}, expected {expected_length}.")
return errors
if __name__ == '__main__':
# Example usage
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8]
lists = [list1, list2, list3]
expected_length = 3
errors = validate_lists(lists, expected_length)
if errors:
print("Validation Errors:")
for error in errors:
print(error)
else:
print("All lists are valid.")
#Example with invalid type
lists = [list1, "not a list", list2]
expected_length = 3
errors = validate_lists(lists, expected_length)
if errors:
print("Validation Errors:")
for error in errors:
print(error)
else:
print("All lists are valid.")
Add your comment