Account
Categories

Python List remove() Method


Definition:

If you need to delete a value from a list, this method removes it from the list. When you delete any value, this method shifts the values to fill the empty position.

Note: It deletes the first occurrence of the value that you want to delete.

Now you will clearly understand the method through the flowchart .

List →  a = [10, 20, 30, 20]

Index :   0      1      2      3  
          ↓      ↓      ↓      ↓
Value :  10     20     30     20  
                 ↑
           (first occurrence)

Method →  a.remove(20)

Removed →  20 ❌

After Remove →  [10, 30, 20]

Index :   0      1      2  
          ↓      ↓      ↓
Value :  10     30     20  

Shift →    3020
            ←      ←
         (left shift)

Syntax

list_name.remove(value)

Example:

a = [10, 20, 30, 20]

a.remove(20)

print(a)

Output:

Exercise

Remove value 15 from the list a = [5, 15, 10, 15] using Python.