Remove list of elements from list Python

In this article we will discuss different ways to remove an elements from list.

Remove an element from List by value using list.remove[]

Pythons list provides a member function to remove an element from list i.e.

list.remove[value]
It removes the first occurrence of given element from the list.

For example,

Suppose we have a list of numbers i.e.

# List of numbers listOfnum = [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56]
Lets remove 56 from the given list using list.remove[] i.e.
# Remove first occurrence of 56 from List listOfnum.remove[56]
It will remove the first occurrence of 56 from the above lists. Lists contents will be now,
[12, 44, 45, 34, 3, 56, 4, 33, 44, 56]
If we try to remove the element that doesnt exists in list then list.remove[] will throw exception.
Therefore before calling list.remove[] we should either,

Check if element exists in list i.e.

# Check if element exist in List, before removing if 99 in listOfnum: listOfnum.remove[99] else: print["Given Element Not Found in List"]
Or use try / except i.e.
# If given element doesn't exists in list, then remove[] can throw Error # Therefore use try / except while calling list.remove[] try : listOfnum.remove[99] except ValueError: print["Given Element Not Found in List"]
Related Articles
  • Python: Remove elements from a list while iterating
  • Python: Remove elements from list by value [first or all occurrences]
  • Python: Remove elements from list by index or indices

Remove an element from List by Index using list.pop[]

list.pop[index]
In python lists pop[] function will remove the element at given index and also returns the deleted element.
If index is not given then it deletes the last element.

For examples,

We have a list of ints i.e.

# List of numbers listOfnum = [12, 44, 12, 56, 45, 34, 3, 56, 4, 33, 44, 56]
Lets remove element at index 2
# Remove the element at index 2 in list value = listOfnum.pop[2]
Lists contents will be now
[12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56]
Returned value is 12 i.e. the deleted element.

list.pop[] and Runtime Error

If list.pop[] is called an index that is out of the boundary of List then it will generate a runtime error.
Therefore we should always check the size of list before calling list.pop[] i.e.

# pop[] can throw error if given index is out of range, so check size before calling it if len[listOfnum] >= 20: listOfnum.pop[20] else: print["Index Out of Range"]
or Use try / except
# pop[] can throw error if given index is out of range, so use try / except try : listOfnum.pop[20] except IndexError: print["Index Out of Range"]

Remove an element from List by del

In python del is a keyword and can be used to delete the element in list by index i.e.

del listOfNum[2]
It will delete the element at index 2 in list.

If del list[index] is called on an index that is out of the boundary of List then it will generate a runtime error.
Therefore check size of list before calling del i.e.

# del list[index] can throw error if given index is out of range, so check size before calling it if len[listOfnum] >= 20: del listOfnum[20] else: print["Index Out of Range"]
or Use try / except
# del list[index] can throw error if given index is out of range, so use try / except try : del listOfnum[20] except IndexError: print["Index Out of Range"]
Complete example is as follows,
def main[]: # List of numbers listOfnum = [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56] ''' Remove an element from List by value using list.remove[] ''' print["*****list.remove[]*****"] print["Original List : " , listOfnum] # Remove first occurrence of 56 from List listOfnum.remove[56] print["Modified List : " , listOfnum] # If given element doesn't exists in list, then remove[] can throw Error # Therefore use try / except while calling list.remove[] try : listOfnum.remove[99] except ValueError: print["Given Element Not Found in List"] # Check if element exist in List, before removing if 99 in listOfnum: listOfnum.remove[99] else: print["Given Element Not Found in List"] ''' Remove an element from List by Index using list.pop[] ''' print["*****list.pop[]*****"] # List of numbers listOfnum = [12, 44, 12, 56, 45, 34, 3, 56, 4, 33, 44, 56] print["Original List : ", listOfnum] # Remove the element at index 2 in list value = listOfnum.pop[2] print["Deleted Element : ", value] print["Modified List : ", listOfnum] # Remove the last element from list value = listOfnum.pop[] print["Deleted Element : ", value] print["Modified List : ", listOfnum] # pop[] can throw error if given index is out of range, so check size before calling it if len[listOfnum] >= 20: listOfnum.pop[20] else: print["Index Out of Range"] # pop[] can throw error if given index is out of range, so use try / except try : listOfnum.pop[20] except IndexError: print["Index Out of Range"] print["*****del list[index]*****"] ''' Remove an element from List by del ''' listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44] print["Original List : ", listOfnum] # Delete element at index 2 del listOfnum[2] print["Modified List : ", listOfnum] # del list[index] can throw error if given index is out of range, so check size before calling it if len[listOfnum] >= 20: del listOfnum[20] else: print["Index Out of Range"] # del list[index] can throw error if given index is out of range, so use try / except try : del listOfnum[20] except IndexError: print["Index Out of Range"] if __name__ == '__main__': main[]
Output:
*****list.remove[]***** Original List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56] Modified List : [12, 44, 45, 34, 3, 56, 4, 33, 44, 56] Given Element Not Found in List Given Element Not Found in List *****list.pop[]***** Original List : [12, 44, 12, 56, 45, 34, 3, 56, 4, 33, 44, 56] Deleted Element : 12 Modified List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56] Deleted Element : 56 Modified List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44] Index Out of Range Index Out of Range *****del list[index]***** Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44] Modified List : [12, 44, 45, 34, 3, 4, 33, 44] Index Out of Range Index Out of Range

Advertisements

Video liên quan

Chủ Đề