Consider the following code snippet.
## Heading ##
def pos(l):
count_positive=0
for i in l:
if i>0:
count_positive+=1
return count_positive
def even(l):
count_even=0
for i in l:
if i%2==0:
count_even+=1
return count_even
list_int=[[2,-3,-6,10],[10,34,26,87],[1,-2,-3,4]]
list_int=sorted(list_int,key=lambda x: (pos(x),even(x)))
print(list_int)
Lambda functions can't have more than one expression.
But in the code above, I am trying to sort the 2D list first based on number of positive elements and then based on number of even elements. Doesn't this mean that there are two expressions in the lambda function? Or is it like, as I have enclosed the two conditions inside a tuple, it would be considered as one single condition?