CodeBlessU
learn code with examples

Python Remove by Value

Use .remove() method to remove item by Value in mutable sequence (List / Array / String). See How to Remove element by Value from Tuple.

Argument is mandatory.

If specified value is not present, it raises ValueError. If specified value is present multiple times, it removes first matching instance from start.

Syntax:
variable.remove(item)
Return Value:
no return value
Use
  • Remove - to remove element by value.
  • Pop - to remove element by index.
  • Clear - to remove all elements.

ValueError

Why this error:

Are you trying remove element that does not exists in sequence?

Solution:

Check whether value is present or not, before removing.


TypeError: remove() takes exactly one argument

Why this error:

Are you trying to call .remove() without any argument or multiple arguments?

Solution:

Call .remove() with one argument - item to be deleted

List Remove by Value

# Example, List remove element by Value
a1 = [10, 20, 30, 40, 50, 20]

a1.remove(30)
print(a1) # [10, 20, 40, 50, 20]

List Remove by Value - Multiple instance of same value

# Example, List Remove element by Value - Multiple instance of same value
a1 = [10, 20, 30, 40, 50, 20]

a1.remove(20)
print(a1) # [10, 30, 40, 50, 20]

List Remove by Value - Matching element not present

# Example, List Remove element by Value - Matching element not present
a1 = [10, 20, 30, 40, 50]

a1.remove(100) # raises ValueError: list.remove(x): x not in list

ValueError: list.remove(x): x not in list

# Example, List remove raises ValueError
a1 = [10, 20, 30, 40, 50]

a1.remove(100) # raises ValueError: list.remove(x): x not in list

TypeError: remove() takes exactly one argument

# Example, list remove raises TypeError
a1 = [10, 20, 30, 40, 50]

a1.remove() # raises TypeError: remove() takes exactly one argument

Tuple Remove by Value

Tuple once created, can not be modified. Alternate approach is to use slicing and concatenation to remove it.
# Example, Remove/delete element by value
a1 = (10, 20, 30, 40, 50, 100)

# remove element 30
if a1.count(30) > 0:
    position = a1.index(30)
    a1 = a1[:position] + a1[position+1:]
else:
    print("Element not present.")

print(a1)

Array Remove by Value

# Example, Array remove element by Value
import array
a1 = array.array('l', [10, 20, 30, 40, 50, 20] )

a1.remove(30)
print(a1) # array('l', [10, 20, 40, 50, 20])

Array Remove by Value - Multiple instance of same value

# Example, Array Remove element by Value - Multiple instance of same value
import array
a1 = array.array('l', [10, 20, 30, 40, 50, 20] )

a1.remove(20)
print(a1) # array('l', [10, 30, 40, 50, 20])

Array Remove by Value - Matching element not present

# Example, Array Remove element by Value - Matching element not present
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.remove(100) # raises ValueError: array.remove(x): x not in list

ValueError: array.remove(x): x not in list

# Example, Array remove raises ValueError
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.remove(100) # raises ValueError: array.remove(x): x not in list

TypeError: remove() takes exactly one argument

# Example, Array remove raises TypeError
import array
a1 = array.array('l', [10, 20, 30, 40, 50] )

a1.remove() # raises TypeError: remove() takes exactly one argument

In this page...

Copyright © All rights reserved.