Skip to content

Python list pop function | Remove element from the list

  • by

Python list pop function is used to removes the element at the given index from the list and it returns the removed item. So basically you can remove the element at the specified position by using the pop() Method in Python List.

Note: Index in Python List starts from 0, not 1.

Syntax

list.pop(index)

Parameter

index (optional) – The value at index is popped out and removed from the list. It takes a single argument.

Note: If the index is not given, then the last element is popped out and removed.

Returns

It returns the item present at the given index. 

Examples of Python list pop

1. Pop item at the given index from the list

List indexing is started from 0, so if you want to remove the first element from the list then you have to pass the 0, for 2 elements pass 1…

In the example of deleting a 2 value, So you have to pass index value 1.

# programming languages list
lang = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 2nd item
return_value = lang.pop(1)
print('Return Value:', return_value)

print('List after removed item:', lang)

Output:

Return Value: Java
List after removed item: [‘Python’, ‘C++’, ‘French’, ‘C’]

2. Python pop “First Element” from the list

To delete the first element from a list you have to just pass the 0 index value in the pop() method.

Python pop Number and String from the list the same way, just need to pass index value.

alpha = ['A', 'B', 'C', 'D', 'E']

# remove first element
return_value = alpha.pop(0)
print('Return Value:', return_value)

print('Updated list:', alpha)

Output:

Return Value: A
Updated list: [‘B’, ‘C’, ‘D’, ‘E’]

3. Python list pop last

list.pop() removes and returns the last element of the list. You don’t need to pass any value to delete the last value of the list in the pop() function.

list1 = [1, 2, 3, 4, 5, 6]
print(list1.pop())
print("New List: ", list1)

Output:

6
New List: [1, 2, 3, 4, 5]

IndexError: pop index out of range

Example of Python3 program for error in pop() method.

list1 = [1, 2, 3, 4, 5, 6]
print(list1.pop(8))

Output:

Python list pop function Remove element

Q: How to Python pop multiple values?

Answer: Using a Using list comprehension, you can remove multiple values from a list.

Note: It’s removed by value not index and we are using a Python remove function.

# creating a list 
list1 = [10, 5, 17, 18, 43, 50]

# elements to be removed
unwanted_num = {17, 43}

list1 = [ele for ele in list1 if ele not in unwanted_num]

# modified list
print("Updated list: ", list1)

Output:

Updated list: [10, 5, 18, 50]

Q: How do Python lists pop by value?

Answer: You can use remove() functionto removes the first matching value, not a specific index.

# creating a list 
list1 = [10, 5, 17, 18, 43, 50]
# remove value 17
list1.remove(17)
# modified list
print("Updated list: ",  list1)

Output:

Updated list: [10, 5, 18, 43, 50]

Do comment if you have doubts and suggestions on this topic.

Note: This example (Project) is developed in PyCharm 2020.1 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.4
Python 3.7
All Python Programs code are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *