+ 1

Smallest number to the right of the number in a list

If i have a list- [9, 0, 7], how would i check if 7 has a smaller number to the right of it, in which case it doesnt, and for 9 it would be 0

4th Apr 2022, 3:21 PM
Kirill
Kirill - avatar
4 ответов
+ 3
Is there possibility of duplicate values in the list? if not, you can try slicing from the index of 7 and use any( ) to check whether there is any number smaller than 7. It might worth considering to assume no such number when number of 7 was the last item.
4th Apr 2022, 3:39 PM
Ipang
+ 2
Ipang what if there are duplicates?
4th Apr 2022, 6:02 PM
Kirill
Kirill - avatar
+ 1
Guess we have to make sure which 7 it was to check ... (Edit) This was what I had in mind, no duplicate value was expected though ... lst = [ 9, 0, 7 ] fun = lambda iter, n : any( val < n for val in iter[ iter.index( n ) : ] ) print( fun( lst, 9 ) ) print( fun( lst, 7 ) )
4th Apr 2022, 6:35 PM
Ipang
0
You can check in this way also p=[9,0,7] m=p.index(7) for i in range(0,m): if(p[i]<p[m]): a=p[i] print(a) Hope you understood this :)
6th Apr 2022, 3:13 AM
UNKNOWN