Home Is there a simple way to delete a list element by value?
Reply: 0

Is there a simple way to delete a list element by value?

user512
1#
user512 Published in July 21, 2018, 8:53 pm
a=[1,2,3,4]
b=a.index(6)
del a[b]
print a

The above shows the following error:

Traceback (most recent call last):
  File "D:\zjm_code\a.py", line 6, in <module>
    b=a.index(6)
ValueError: list.index(x): x not in list

So I have to do this:

a=[1,2,3,4]
try:
    b=a.index(6)
    del a[b]
except:
    pass
print a

But is there not a simpler way to do this?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.433657 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO