want to remove a value from a list if it exists in the list (which it may not).

```a = [1, 2, 3, 4]
b = a.index(6)

del a[b]
print(a)
```

The above case (in which it does not exist) 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?

Dec 28, 2020 in Python 409 views

## 3 answers to this question.

To remove an element's first occurrence in a list, simply use list.remove:

```>>> a = ['a', 'b', 'c', 'd']
>>> a.remove('b')
>>> print(a)
['a', 'c', 'd']
```

Mind that it does not remove all occurrences of your element. Use a list comprehension for that.

```>>> a = [10, 20, 30, 40, 20, 30, 40, 20, 70, 20]
>>> a = [x for x in a if x != 20]
>>> print(a)
[10, 30, 40, 30, 40, 70]```
• 65,930 points

The effects of the three different methods to remove an element from a list:

remove removes the first matching value, not a specific index:

```>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
```

del removes the item at a specific index:

```>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
```

and pop removes the item at a specific index and returns it.

```>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
```

Their error modes are different too:

```>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range```
answered Dec 28, 2020 by Reshma

There are three ways in which you can Remove elements from List:

1. Using the remove() method.
2. Using the list object's pop() method.
3. Using the del operator.

In Pythonlist 's methods clear() , pop() , and remove() are used to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

answered Dec 28, 2020 by Thomas Walenta

## Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

## Is there a way to loop between 0 and 1 by 0.1 in python?

You can use the linespace function. It ...READ MORE

## Is there a way to store this text in a list using selenium (python)

Try using this code snippet to resolve ...READ MORE

## Deleting a list element by value

Use the remove() function: >>> p = [1, ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE