How To Remove an Element From a List by Index in Python

Remove an element from a list by index in python

To remove an element from a list by index in Python, there are four solutions we have effectively tested. Follow the article to understand better.

Remove an element from a list by index in python

Using the pop() method

The pop() method removes the element at the specified position.

Syntax:

list.pop(pos)

Parameter:

  • pos: a number that returns the position of the element you want to remove. The default is -1. Optional parameter.

Example:

  • Create a list
  • Use list.pop() function to access each specific index of the list, and the function will remove it.
listName = ['John', 'Peter', 'Frank']

# Use the pop() method to remove an element from the the list
x = listName.pop(0)

print(listName)

Output:

['Peter', 'Frank']

The element ‘John’ has been removed.

Note: The pop() method returns the removed value.

Using the remove() method

The remove() method is used to remove the element that appears for the first time in the list.

Syntax:

list.remove(element)

Parameter:

  • element: the element you want to remove. Required parameter

Example :

  • Create a list
  • Use the remove() function to find the first element in the list with the same value as the specified argument and remove it from the list.
listName = ['John', 'Peter', 'John', 'Frank']

# Use the remove() method to remove the element in the list
x = listName.remove('John') 

print(listName)

Output:

['Peter', 'John', 'Frank']

Note: The remove() method only removes the first element from left to right in the list if that element exists only.

Using ‘del’ keyword

Python uses the ‘del’ keyword to remove objects (variables, lists, list elements,etc) in Python. The del command helps us to delete elements in a specified range in the list. This range will be specified how to use the slice list function in python.

Syntax:

del list [ index ]

Parameters:

  • list: list of elements to delete.
  • index: location to delete.

Example:

  • Create a list
  • Use the del keyword, the input parameter is the index of the position to be deleted in the list.
listName = ["John", "Peter", "Frank"]

# Use the 'del' keyword to remove the elements
del listName[1]

print(listName)

Output:

['John', 'Frank']

You can also delete multiple elements within a specified range with the del command:

del list [start : stop]

Example:

listName = ["John", "Peter", "Frank", "Tiff", "Peter"]

# Use the 'del' keyword to remove the elements
del listName[1:3]

print(listName)

Output:

['John', 'Tiff', 'Peter']

Using the clear() method

If you want to remove all elements, use the clear() method.

Syntax:

list.clear()

Parameters: no parameters

Example:

  • Create a list
  • The clear() function will output all the elements contained in the list, but elements in list will not be deleted, elements just emptied.
listName = ["John", "Peter", "Frank"]

# Use the clear() function to remove all elements
listName.clear()

print(listName)

Output:

[]

All the elements have been removed.

Note: the list object is not deleted but emptied.

Summary

Here are the solutions that can help you remove an element from a List by index in Python. If you have any questions about this topic, please comment below. I will answer your questions. Thanks for reading!

Maybe you are interested:

Leave a Reply

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