Estimated reading time: 1.6 mins read
What is pop()?
- Removes an item by index (lists) or key (dictionaries)
- Returns the removed item
- Raises an error if the index/key does not exist
Example with List:
fruits = ["Apple", "Banana", "Cherry"]
removed_fruit = fruits.pop(1) # Remove item at index 1
print("Removed fruit:", removed_fruit)
print("Updated list:", fruits)
Output:
Removed fruit: Banana
Updated list: ['Apple', 'Cherry']
Example with Dictionary:
user = {"name": "Nishtha", "age": 25}
removed_age = user.pop("age")
print("Removed age:", removed_age)
print("Updated dictionary:", user)
Output:
Removed age: 25
Updated dictionary: {'name': 'Nishtha'}
What is remove()?
- Removes an item by value (lists only)
- Does NOT return anything
- Raises ValueError if the value is not found
- ❌ Not available for dictionaries
Example:
numbers = [10, 20, 30, 20]
numbers.remove(20) # Remove first occurrence of 20
print("Updated list:", numbers)
Output:
Updated list: [10, 30, 20]
Key Differences Between pop() and remove()
| Feature | pop() | remove() |
|---|---|---|
| Works with | Lists, Dictionaries | Lists only |
| Removes by | Index (list), Key (dict) | Value |
| Returns value | ✅ Yes | ❌ No |
| Raises error if item not found | IndexError / KeyError | ValueError |
| Can be used with dictionary | ✅ Yes | ❌ No |
Important Interview Qs&As
Q1: Can you use remove() with a dictionary?
remove() with a dictionary?A1: No, remove() works only with lists. Use pop() or del to remove items from dictionaries.
Q2: What does pop() return?
pop() return?A2: pop() returns the removed value from a list or dictionary.
Q3: What happens if you try to pop() a key that doesn’t exist in a dictionary?
pop() a key that doesn’t exist in a dictionary?A3: Python raises a KeyError. You can provide a default value to avoid the error:
user.pop(“phone”, “Not found”)
Q4: How is remove() different from pop() in lists?
remove() different from pop() in lists?A4: remove() deletes by value and does not return anything, while pop() deletes by index and returns the removed item.
Q5: Can pop() be used without specifying a key or index?
pop() be used without specifying a key or index?- For lists,
pop()without index removes the last item. - For dictionaries,
pop()requires a key.
Happy Learning !


Leave a Reply