List pop in Python is a pre-defined, in-built function that removes an item at the specified index from the list. You can also use pop in Python without mentioning the index value. In such cases, the pop() function will remove the last element of the list.

What Is the Syntax of List Pop In Python?

The pop() method’s syntax is:

list_name.pop(index)

Parameters:

index: The Python pop() function accepts only a single parameter which is the item’s index to be popped out of the list. It is an optional parameter.

If you specify an out-of-range index, then the compiler throws an IndexError exception.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Value Does Pop In Python Return?

The list pop in Python returns the popped value, or in other words, the item present at the specified index value. It is simpler to understand this with an example.

Example: Use Pop In Python to Remove an Item and Return It

In this code, you will create a list of fruits and then use the Python list pop() function to remove an item at a specified index. You will then store the returned value in a variable and print it to see what the pop() function returns.

# Cars list

cars = ['Mercedes Benz', 'BMW', 'Jeep', 'Mahindra', 'Maserati']

print(cars)

# Using pop() and storing the return value

ret_val = cars.pop(2)

print('The return value is:', ret_val)

# Updated List

print('The updated list is:', cars)

Output:

/PopinPython_1

As you can see in the output, pop in Python returns the popped item. You can also print the return value by merely printing the pop code, instead of storing it in a separate variable, like this:

# Cars list

cars = ['Mercedes Benz', 'BMW', 'Jeep', 'Mahindra', 'Maserati']

print(cars)

# Using pop() and printing the return value

print ("The return value is:", cars.pop(2))

# Updated List

print('The updated list is:', cars)

Output:

PopinPython_2

Note: The pop() function removes the third item from the list as the indexing starts with 0. So when you pass 2 as the argument, it will remove the third item.

More Examples For List Pop In Python

This article contains more examples to help you better understand the Python list pop() function.

Example: Using Pop In Python Without Passing Index

In this code, you will use the same car list. But this time, you won’t pass the index argument.

# Cars list

cars = ['Mercedes Benz', 'BMW', 'Jeep', 'Mahindra', 'Maserati']

print(cars)

print ("The return value is:", cars.pop())

# Updated List

print('The updated list is:', cars)

Output:

PopinPython_3

As you can see in the above output, when you don’t pass an index value, the list pop() function by default removes the last item in the list.

Example: Using the Python List Pop() Function With Negative Indices

For this example, you will pass negative index values and see what happens to the car list.

# Cars list

cars = ['Mercedes Benz', 'BMW', 'Jeep', 'Mahindra', 'Maserati']

print(cars)

print ("The return value is:", cars.pop(-2))

# Updated List

print('The updated list is:', cars)

print ("The return value is:", cars.pop(-1))

# Updated List

print('The updated list is:', cars)

Output:

PopinPython_4.

As you can see from the output, if you pass negative indices, the pop in Python will start counting from the right. But this time, it won’t start from 0 and then -1; it will directly begin from -1.

Example: Using Out-of-Range Value to Get IndexError Exception

In the below program, you will pass an argument larger than the range to get an IndexError exception in the output.

# Cars list

cars = ['Mercedes Benz', 'BMW', 'Jeep', 'Mahindra', 'Maserati']

print(cars)

print ("The return value is:", cars.pop(8))

# Updated List

print('The updated list is:', cars)

Output:

/PopinPython_5.

What Is Python Dictionary Pop?

Like the list pop in Python, the dictionary pop() function removes an element mentioned at the specified index value from a dictionary.

The Syntax of the Python Dictionary Pop() Function

The syntax of dictionary pop is:

dictionary.pop(key[, default])

The parameters are:

  • key: As the dictionary works in a key: value pair, it is the key value to be removed
  • default: The default value is to be returned if the key is not found. It is an optional parameter

What Value Does Dictionary Pop In Python Return?

The Python dictionary pop() return value is:

  • If it finds the key, it returns the popped value to which the specified key belongs
  • If it does not find the key, it returns the default value
  • If the key is not found and the default is not specified, it returns a KeyError exception

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

Examples of Python Dictionary Pop() Function

Let’s look at different examples to understand the functioning of dictionary pop and its value in different scenarios.

Example: Using Dictionary Pop In Python when the Key Is Present

Continuing with the cars, you will create a key: value pair for each vehicle to define a dictionary and then use the pop() function to remove one of the items.

# Cars Dictionary

cars = {'Mercedes Benz': 1, 'BMW': 2, 'Jeep': 3, 'Mahindra': 4, 'Maserati': 5}

print(cars)

print ("The return value is:", cars.pop('Mahindra'))

# Updated Dictionary

print('The updated dictionary is:', cars)

Output:

PopinPython_6

Example: Using the Python Dictionary Pop() Function When Default Value Is Provided

In the below code, you will pass a key that is not present in the dictionary. But you will also give a default value so that the pop() function can return the default value.

# Cars Dictionary

cars = {'Mercedes Benz': 1, 'BMW': 2, 'Jeep': 3, 'Mahindra': 4, 'Maserati': 5}

print(cars)

x = cars.pop('Ferrari', 'Lamborghini')

print('The popped element is:', x)

print('The dictionary is:', cars)

Output:

PopinPython_7.

As you can see in the output, since the key Ferrari is not present in the dictionary, it returns the default value, Lamborghini.

Example: Using Dictionary Pop in Python To Get Keyerror Exception

In the below code, you will pass the key that is not present in the dictionary. But this time, you will not give a default value. Thus, the compiler will return a KeyError Exception.

# Cars Dictionary

cars = {'Mercedes Benz': 1, 'BMW': 2, 'Jeep': 3, 'Mahindra': 4, 'Maserati': 5}

print(cars)

print(cars.pop('Ferrari'))

Output:

PopinPython_8.

As the Ferrari key is not present in the dictionary and you have passed no default parameter, it throws the KeyError Exception.

Summing it Up

In this article, you learned about pop in Python, its uses, and its two variants: list pop() and dictionary pop(). It is an essential function in Python. You can learn more about functions basics here

If you want to start your journey on becoming a Full Stack Developer, then Simplilearn's got the perfect course for you! Check out our Post Graduate Program In Full Stack Web Development designed in collaboration with Caltech to help you learn the complete ins and out of software development.

Do you have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same as soon as possible.

Happy learning!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449