Reputation: 469
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')
Upvotes: 3
Views: 53
Reputation: 8520
as additional note, if you want to know if every element in the list have the same length regardless of its value, you can use
len( set( len(x) for x in my_list ) ) == 1
with set you eliminate all duplicates so if in the end if its length is more than one then some stuff there have different size
Upvotes: 0
Reputation: 5289
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
Upvotes: 3