Delete Particular Or All Elements From A List


How to delete remove the particular or all elements from a list?

  • We can delete one or more items from a list using the keyword del.
  • We can use the remove ( ): method to remove a particular or pop () method to remove an item at the given index.
  • The pop ( ): method removes and returns the last item if the index is not given. This helps us implement lists as stacks that use LIFO(Last in, First out) structure.
  • We can not remove duplicate elements from the list by removing one duplicate element or declaring one element. The remove() method will delete the first coming element from the list only.
  • If the list has no element x and you try to remove x with the help of the remove() method, then it will show ValueError:list.remove(x):x not in list exception.
  • We can clear all the list elements with the help of the clear() method.

Example:

weblist = ["knoweldge2life 1", "knoweldge2life 2", "knoweldge2life 3"] weblist.remove("knoweldge2life 2") print(weblist)

OUTPUT:

['knoweldge2life 1', 'knoweldge2life 3']