Guidelines

Removing Elements with the del Keyword and pop() Function

To remove an element from a specific index in a list, you can use the del keyword or the pop() function.


Using the del Keyword

The del keyword removes the element at the specified index from the list.

Example of del Keyword
fruits = ['Apple', 'Banana', 'Cherry'] del fruits[1] # Remove the element at index 1 (second element) print("fruits:", fruits) # ['Apple', 'Cherry']

In the code above, del fruits[1] removes the 'Banana' from the fruits list at index 1 (the second element).


Using the pop() Function

The pop() function removes the element at a specific index within parentheses (), and returns its value.

If no index is specified, it removes and returns the last element of the list.

Example of pop() Function
numbers = [1, 2, 3, 4, 5] last_number = numbers.pop() print("last_number:", last_number) # 5 print("numbers:", numbers) # [1, 2, 3, 4] first_number = numbers.pop(0) print("first_number:", first_number) # 1 print("numbers:", numbers) # [2, 3, 4]
Mission
0 / 1

Remove Element with pop()

How do you remove 'apple' using the pop() function from the list below? Expected output: removed: apple

fruits = ['apple', 'banana', 'cherry']


removed = fruits.pop


print('removed:', removed)

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result