To remove an element at a specific index from a list in Python, you can use the pop method of list object.
In this example,
numbers
with integer values.pop
method of the list to remove the element at the specified index.numbers = [1, 2, 3, 4, 5]
index_to_remove = 2
numbers.pop(index_to_remove)
print("List after removing element at index", index_to_remove, ": ", numbers)
List after removing element at index 2 : [1, 2, 4, 5]
In this example,
words
with string values.pop
method of the list to remove the element at the last index.words = ["apple", "banana", "cherry"]
words.pop()
print("List after removing element at last index: ", words)
List after removing element at last index: ['apple', 'banana']
In this tutorial, we learned How to Remove Element at a Specific Index from a List in Python language with well detailed examples.