Skip to main content

Remove Specified Item

To take out an item by its value, use the remove() method.

When that value appears more than once, only the earliest match is removed:

Example

Take "banana" out of the list:

Prompt
Create a list of fruits and use the remove() method to remove "banana". Print the list.
Output: complete Python program

Example

Only the first "banana" goes:

Prompt
Create a list with duplicate "banana" entries. Use remove() to remove the first occurrence of "banana". Print the list.
Output: complete Python program

Remove Specified Index

To remove by position, give pop() the index.

Call pop() with no index and it removes the final item instead.

The del keyword can also remove the item at a given index:

Beyond that, del can delete a whole list.

Example

Drop the second item:

Prompt
Create a list of fruits and use the pop() method to remove the item at index 1. Print the list.
Output: complete Python program

Example

Drop the final item:

Prompt
Create a list of fruits and use the pop() method without an index to remove the last item. Print the list.
Output: complete Python program

Example

Drop the first item using del:

Prompt
Create a list of fruits and use the del keyword to remove the first item. Print the list.
Output: complete Python program

Example

Delete the whole list:

Prompt
Create a list of fruits and use the del keyword to delete the entire list.
Output: complete Python program

Clear the List

To empty a list, call clear().

The list still exists afterward, just with nothing in it.

Example

Empty the list and print it:

Prompt
Create a list of fruits and use the clear() method to empty the list. Print the list.
Output: complete Python program