-1

I've tried with this code but it doesn't work for a list, why?

def removing(a):
    for x in a:
        if x < a[0]:
           a.remove(x)
    return (a)
khelwood
  • 52,115
  • 13
  • 74
  • 94
uau321
  • 1
  • Do a bit of research on why removing items from a list while iterating, will give unexpected results. – S3DEV May 19 '22 at 21:29
  • 1
    Changing a list while iterating over the same list is a no-no. Instead, you can use something like list comprehension to create a new one from your condition: `[x for x in a if x < a[0]]`. Then just `del a`. There are some caveats here though depending on the object type `a` is holding. – Chrispresso May 19 '22 at 21:33

0 Answers0