Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Remove Element From Given Index – pop() Function with Examples

pop() function is used to remove the item at the given position (i – parameter) in a list. Remember i is the index of the element which is a position where that number exists and not the actual number.

Suppose, there is a list (a, b, c, d, e, f) and for removing c from the list we have to pass its index number which is ‘2‘. And pop() method will remove that element from the list. Remember, index of the element starts from the 0 not 1.

Syntax of pop() Function

The syntax of pop() function in Python is:

 

Parameters of pop() Function in Python

i – Where i is the index which we pass as an argument to the method. This parameter is optional. If there is no parameter passed to the pop() then the default index is (-1) which will return the last item of the list.

 

Compatibility

pop() function is available and compatible with both Python 2.x and 3.x. This function is one of python list methods.

 

Return Value of pop() Function in Python

pop() function returns the item of the index which passed as an argument and remove the item at that given index position and update the list with these changes.

 

Python pop() Function Example 1

There is a list of colors (red, green, blue) and goal is to remove ‘green’ from the list by using pop() method and show the return value of pop() method.

Output:
value on the index: green
Updated color list ['red', 'blue']

 

Python pop() Function Example 2

When we don’t pass the argument index then last value of the list (blue) will be removed.

Output:
value on the index: blue
Updated color list ['red', 'green']

LearnAndLearn.com © 2018 - All rights reserved