Back to Python
2026-04-245 min read

pop() (Python Programming)

Learn pop() (Python Programming) step by step with clear examples and exercises.

Why This Matters

In this full guide, we delve into the pop() method for Python lists, a crucial tool that enables the removal and retrieval of items from your list. Mastering pop() is essential for excelling in programming interviews, debugging real-world code, and tackling complex programming tasks.

Prerequisites

Before diving into the intricacies of pop(), it's important to have a solid understanding of the following:

  1. Basic Python syntax
  2. Familiarity with Python lists
  3. Comprehension of indexing and slicing in Python lists
  4. Understanding of list comprehensions and methods like append(), extend(), and sort()
  5. Knowledge of conditional statements (if-else) and loops (for, while)
  6. Familiarity with functions and their parameters
  7. Awareness of error handling using try-except blocks

Core Concept

The pop() method is a built-in function for Python lists that removes an item at the specified position and returns it. It's a versatile tool that can help you manipulate your list dynamically, making it easier to work with data structures in your code.

Syntax

list_name.pop()

By default, pop() removes the last item from the list and returns it. You can specify an index to remove a different element:

list_name.pop(index)

If you try to access an index that's out of range, Python will throw an IndexError.

Example

Let's create a list and demonstrate the pop() method in action:

programming_languages = ['Python', 'Java', 'C++', 'French', 'C']

Remove and return the 4th item (French)

returned_language = programming_languages.pop(3)

print('Return Value:', returned_language)

Updated List

print('Updated List:', programming_languages)

Output:

Return Value: French

Updated List: ['Python', 'Java', 'C++', 'C']


### Using pop() with Lists of Different Data Types
Note that that when you use `pop()` on a list containing different data types, the returned value will be the element at the specified index (not necessarily the last one). For example:

mixed_list = [1, 'apple', 3.14, True]

Remove and return the 2nd item ('apple')

returned_item = mixed_list.pop(1)

print('Return Value:', returned_item)

Updated List

print('Updated List:', mixed_list)

Output:

Return Value: apple

Updated List: [1, 3.14, True]


### pop(last=True) - Optional last Argument
By default, `pop()` removes the last item from the list when no index is specified. However, you can change this behavior by setting the optional `last` parameter to `False`. This will remove and return the first item instead:

programming_languages = ['Python', 'Java', 'C++', 'French', 'C']

Remove and return the first item ('Python')

returned_language = programming_languages.pop(0, last=False)

print('Return Value:', returned_language)

Updated List

print('Updated List:', programming_languages)

Output:

Return Value: Python

Updated List: ['Java', 'C++', 'French', 'C']

Worked Example

Let's consider a scenario where you have a list of students and their scores, and you want to remove the student with the lowest score.

students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
scores = [90, 85, 70, 65, 88]

Find the index of the student with the lowest score

lowest_score_index = scores.index(min(scores))

Remove the student with the lowest score and print their name

removed_student = students.pop(lowest_score_index)

print('Removed Student:', removed_student)

Print the updated list of students

print('Updated Students List:', students)

Output:

Removed Student: David

Updated Students List: ['Alice', 'Bob', 'Charlie', 'Eve']

Common Mistakes

  1. Forgetting to specify an index: If you don't provide an index, pop() will remove the last item by default. Make sure to include an index if you want to remove a specific element.
  2. Accessing an out-of-range index: Be careful when providing an index, as Python will throw an IndexError if you try to access an index that's not present in the list.
  3. Not handling exceptions: Make sure to handle exceptions gracefully when using pop(), especially when removing elements based on conditions like finding the minimum score.
  4. Using pop() with empty lists: Attempting to pop() from an empty list will throw an IndexError. To avoid this, you can check the length of the list before calling pop().
  5. Misunderstanding the last parameter: Remember that when no index is specified, pop() defaults to removing the last item only if the last parameter is set to True. If last=False, it will remove and return the first item instead.

Practice Questions

  1. Write a Python script that removes all occurrences of the number 7 from the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 7, 0].
  2. Given a list of strings, write a function that removes and returns the longest string in the list. If there are multiple longest strings, return any one of them.
  3. Write a script that finds the second highest number in a list of numbers. If there is no second highest number (i.e., all numbers are unique), print "No Second Highest Number".
  4. Create a function that removes all occurrences of a specific value from a dictionary. For example, given the dictionary {'a': 1, 'b': 2, 'c': 3, 'a': 4}, the function should return {'b': 2, 'c': 3} after removing all instances of 'a'.
  5. Write a script that finds and removes duplicates from a list of strings, keeping only the first occurrence of each string. For example, given the list ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'], the script should return ['apple', 'banana', 'orange'].

FAQ

  1. What happens if I try to pop an empty list?
  • Attempting to pop() from an empty list will throw an IndexError. To avoid this, you can check the length of the list before calling pop().
  1. Can I use pop() with other data structures like dictionaries or sets in Python?
  • No, pop() is specifically designed for lists and doesn't work with other data structures like dictionaries or sets in Python. For these, you can use the popitem() method instead.
  1. Is it possible to replace an item at a specific index using pop()?
  • No, the pop() method removes an item and returns it, but it doesn't provide a way to replace the removed item directly in the list. To replace an item at a specific index, you can use the assignment operator (=) with the desired value. For example:
my_list[index] = new_value
pop() (Python Programming) | Python | XQA Learn