I need to validate if my list of lists has equally sized lists.
This should pass. It has two lists, both of length 2:
myList1 = [[1, 1], [1, 1]]
This should pass, it has three lists, all of length 3:
myList2 = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
This should pass, it has three lists, all of length 2:
myList3 = [[1, 1], [1, 1], [1, 1]]
This should fail. It has three lists, one of which is different than the others:
myList4 = [[1, 1], [1, 1, 1], [1, 1, 1]]
I could write a loop to iterate over the list and check the size of each sub-list. Is there a more pythonic way to achieve the result?
all(len(i) == len(myList[0]) for i in myList)
To avoid incurring the overhead of len(myList[0]) for each item, you can store it in a variable
len_first = len(myList[0]) if myList else None
all(len(i) == len_first for i in myList)
If you also want to be able to see why they aren't all equal
from itertools import groupby
groupby(sorted(myList, key=len), key=len)
Will group the lists by the lengths so you can easily see the odd one out