pop(), remove(), clear(), and del
pop(), remove(), clear(), and del with 5 examples each, including slicing where applicable:
1. pop([index])
Removes and returns the item at the given index. If no index is given, it removes the last item.
Examples:
- Remove the last element (default):pythonnums = [1, 2, 3] popped = nums.pop() print(nums) # [1, 2] print(popped) # 3
- Remove at a specific index:pythonfruits = [“apple”, “banana”, “cherry”] removed = fruits.pop(1) print(fruits) # [“apple”, “cherry”] print(removed) # “banana”
- Using negative index (-1 = last element):pythonletters = [‘a’, ‘b’, ‘c’] letters.pop(-1) # Removes ‘c’ print(letters) # [‘a’, ‘b’]
- Using slicing to simulate
pop():pythonnums = [10, 20, 30] popped = nums[-1] # Get last element nums = nums[:-1] # Remove last element print(nums) # [10, 20] print(popped) # 30 - Error if index is out of range:pythonlst = [1, 2] lst.pop(5) # IndexError: pop index out of range
2. remove(x)
Removes the first occurrence of the specified value x. Raises ValueError if not found.
Examples:
- Remove a string:pythonfruits = [“apple”, “banana”, “cherry”] fruits.remove(“banana”) print(fruits) # [“apple”, “cherry”]
- Remove a number:pythonnums = [1, 2, 3, 2] nums.remove(2) # Removes first ‘2’ print(nums) # [1, 3, 2]
- Error if element doesn’t exist:pythonlst = [1, 2, 3] lst.remove(4) # ValueError: list.remove(x): x not in list
- Using slicing +
remove():pythonnums = [10, 20, 30, 20] nums = nums[:2] + nums[3:] # Removes nums[2] (30) print(nums) # [10, 20, 20] - Remove all occurrences (using loop):pythonnums = [1, 2, 3, 2, 2] while 2 in nums: nums.remove(2) print(nums) # [1, 3]
3. clear()
Removes all elements from the list, making it empty.
Examples:
- Basic usage:pythonnums = [1, 2, 3] nums.clear() print(nums) # []
- Using slicing to clear:pythonlst = [10, 20, 30] lst[:] = [] # Same as clear() print(lst) # []
- Difference with
del:pythonlst1 = [1, 2, 3] lst1.clear() # lst1 = [] (still exists) lst2 = [1, 2, 3] del lst2[:] # Same as clear() print(lst2) # [] - Using
*=to clear (not recommended):pythonlst = [1, 2, 3] lst *= 0 # Clears the list print(lst) # [] - Reassigning vs
clear():pythonlst = [1, 2, 3] lst = [] # Creates a new list (old may still exist in memory) lst.clear() # Modifies existing list
4. del Statement
Deletes elements by index or slice (not a method, but a Python keyword).
Examples:
- Delete by index:pythonnums = [10, 20, 30] del nums[1] print(nums) # [10, 30]
- Delete a slice:pythonlst = [‘a’, ‘b’, ‘c’, ‘d’] del lst[1:3] print(lst) # [‘a’, ‘d’]
- Delete the entire list:pythonlst = [1, 2, 3] del lst # Deletes the list completely print(lst) # NameError: name ‘lst’ is not defined
- Using
delwith slicing (similar toclear()):pythonnums = [1, 2, 3] del nums[:] # Clears the list print(nums) # [] - Delete every alternate element:pythonnums = [1, 2, 3, 4, 5] del nums[::2] # Removes 1, 3, 5 print(nums) # [2, 4]
Summary Table
| Method | Modifies List? | Removes By | Returns Removed? | Error if Missing? |
|---|---|---|---|---|
pop([i]) | ✅ Yes | Index | ✅ Yes | IndexError |
remove(x) | ✅ Yes | Value | ❌ No | ValueError |
clear() | ✅ Yes | All | ❌ No | ❌ No |
del | ✅ Yes | Index/Slice | ❌ No | IndexError |