2

I would like to ensure that all my elements of a list of lists have the same length.

I tried:

assert len((map(len,motifs))) == len(motifs[0])

Then:

assert all(len(m[i]) == len(m[0]) for i, m in enumerate(motifs))

Do you have any clean and quick suggestion?

1
  • 1
    m is the sublist, so m[i] and m[0] don't make sense - it should be m and motifs[0].
    – jonrsharpe
    Commented Aug 11, 2019 at 10:33

2 Answers 2

2
assert all( [len(item) == len(motifs[0][0]) for sublist in motifs for item in sublist] )
1
l = [...]
length = <Specified length or len(l[0])>
assert all(len(x) == length for x in l)

You may use for the length parameter any predefined value, or simply you can use the length (using len) of any item in your list of list.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.