Problem: Thou Shalt Not Modify A List During Iteration
Summary: When we remove the first element containing 19, the rest "slide down". For example, when we removed arr[2]
aka ['Varun', 19]
, ['Kakunami', 19]
replaced it. Then, the loop continued to arr[3]
which is now ['Vikas', 21]
. This, in a way, left ['Kakunami', 19]
out of the loop.
Solution: To work around this, just loop over the list from the reverse order: (Have to use a while
...I think)
arr=[['Harsh', 20], ['Beria', 20], ['Varun', 19], ['Kakunami', 19], ['Vikas', 21]]
arr.sort(key=lambda x: (x[1],x[0]))
min_value=min(arr,key=lambda x:x[1])
min_marks=min_value[1]
i = len(arr) - 1;
while i >= 0:
if arr[i][1]==min_marks:
arr.remove(arr[i])
i = i - 1
print arr
A repl.it as a demo
You could do this recursively:
arr=[['Harsh', 20], ['Beria', 20], ['Varun', 19], ['Kakunami', 19], ['Vikas', 21]]
arr.sort(key=lambda x: (x[1],x[0]))
min_value=min(arr,key=lambda x:x[1])
min_marks=min_value[1]
def removeLowest(arr, min):
for i in arr:
if i[1]==min_marks:
arr.remove(i)
return removeLowest(arr, min)
return arr
removeLowest(arr, min)
print arr
Otherwise, there are many other alternatives :)