Cara menggunakan pop multiple elements python

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.

Python Training Course

Learn Data Operations in PythonExplore Course

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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:

Cara menggunakan pop multiple elements python

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

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

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. 

Further, if you want to understand other basic concepts in Python, you can refer to Simplilearn’s Introduction to Python Basics. The article is a compilation of all the basic concepts of the Python programming language. You can also opt for our Online Python Certification Course if you want to excel in the Python development field. The course has several hours of applied learning resources to help you understand both the basic and advanced Python concepts.

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!

About the Author

Cara menggunakan pop multiple elements python
Ravikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.