Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that each list in a list of list is the same length

Tags:

python

list

I have a list and a list of list

A=["grp 1", "grp 2"]
B=[["1","2"],["3","4"],["5","6"]]

how do I check that each list in B is equal to the length of A?

I would like something like

if len(A) != len(list in B):
     raise ValueError('special error message')
like image 377
dana111 Avatar asked Dec 08 '25 23:12

dana111


1 Answers

If you want to make sure that every single element of B is not equal to the length of A then you can use:

a_len = len(A)
all(len(x) != a_len for x in B)

Alternatively you can use the following if you want to see if any element of B is not the same length as A:

a_len = len(A)
any(len(x) != a_len for x in B)

So in your case you could use:

a_len = len(A)
if any(len(x) != a_len for x in B):
    raise error
like image 84
MrAlexBailey Avatar answered Dec 11 '25 21:12

MrAlexBailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!