I want to compare the amount of values in a list of a list. If there would be an empty list in the one of the "sub" lists, it should print "too short". If the list is long enough, meaning that it has more or 1 value in it, it should print "good". how can I do this? No library imports please.
CodePudding user response:
You can iterate through the sub-lists and check for len
, or just for falsey for empty lists as Python evaluates []
as False
.
lists = [[1,2],[],[3,4,5]]
for l in lists:
if len(l) < 1: #or just `if not l:`
print('too short')
else:
print('good')
CodePudding user response:
You need to iterate each element in the list and compare the length using the len function.
Please see sample code below:
#list with one element sublist and 3 element sublist
a = [[1],[3,4,5]]
for index_length in a:
if len(index_length) > 1:
print("Good")
else:
print("too short")