0

Given my List:

arr=[['Harsh', 20], ['Beria', 20], ['Varun', 19], ['Kakunami', 19], ['Vikas', 21]]

I have to remove elements containing lowest numbers

i.e elements:

['Varun', 19], ['Kakunami', 19]

I have tried the following code:

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]
for i in arr:
    if i[1]==min_marks:
        arr.remove(i)

but this is not giving the desired output

Chris Happy
  • 7,088
  • 2
  • 22
  • 49

4 Answers4

0

If you can create a new list then this will work:

arr = [['Harsh', 20], ['Beria', 20], ['Varun', 19], ['Kakunami', 19], ['Vikas', 21]]
min_number = min(arr, key=lambda x: x[1])[1]
arr = [pair for pair in arr if pair[1] != min_number]

Otherwise you may want to look at some of the answers here: How to remove items from a list while iterating?

kingkupps
  • 3,284
  • 2
  • 16
  • 28
0

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 :)

Chris Happy
  • 7,088
  • 2
  • 22
  • 49
0

Without modifying your code too much, this is possible to achieve using list comprehension.

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]

arr = [ elem for elem in arr if elem[1] != min_marks] 

It's not good to remove elements from a list like this whilst you're iterating over it. Instead, you may want to form a list of indicies to remove and use the .remove() functionality.

Nunnsy
  • 137
  • 2
  • 11
0

Here's a simple solution I could think of:

arr=[['Harsh', 20], ['Beria', 20], ['Varun', 19], ['Kakunami', 19], ['Vikas', 21]]

lowest = sorted(set([i[1] for i in arr]))[0]

output = [i for i in arr if i[1] != lowest]

It simply checks for the lowest value in arr, and removes the list containing lowest value from final output

SidK
  • 1,174
  • 1
  • 9
  • 9