Account
Categories

Python List pop() Method


Definition:

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

Note: This method deletes the value at the specified index. If you do not give the index, then it deletes the last value.

Now you will clearly understand the method through the flowchart .


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

Index :   0      1      2      3  
          ↓      ↓      ↓      ↓
Value :  10     20     30     40  
                  ↑
             (target index)

Method →  a.pop(1)

Removed →  20 ❌

After Pop →  [10, 30, 40]

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

Shift →    3040
            ←      ←
         (left shift)

Syntax

list_name.pop(index)

Example:

a = [10, 20, 30, 40]
a.pop(1)
print(a)

Output:

Exercise

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