Reputation: 1866
I have this if statement in python. where if U<0
i want to pass. but if U>0 I want to know if len(U[0])<1
. However I get an error when len(U) < 0
on the line if len(U[0])<1:
IndexError: index out of bounds
. but i thought it was meant to pass this so it should only look for it when len(U)>0
. can anyone help?
if len(U) < 0:
pass
else:
if len(U[0])<1:
pass
else:
Uli=U[0]
list2.append(Uli)
Upvotes: 0
Views: 61
Reputation: 14519
len
returns a non-negative integer. Comparing len(U) < 0
will always be false, so pass
is never executed.
Instead, it goes into the else
branch, where it tries to access the 0
th element of the possibly empty list. The error you're seeing is because the list is empty.
Consider changing if len(U) < 0:
to if len(U) == 0:
or simply if len(U):
or if not U:
.
Upvotes: 1