Removing an element by index from a list is a common operation in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to remove an element at a specific index efficiently.
Example 1:
fruits = ['apple', 'banana', 'orange', 'kiwi'] index = 2 fruits.pop(index) print(fruits)
Output: [‘apple’, ‘banana’, ‘kiwi’]
Example 2:
numbers = [1, 2, 3, 4, 5] index = 3 numbers = numbers[:index] + numbers[index + 1:] print(numbers)
Output: [1, 2, 3, 5]