Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Python: Remove List items

Profile picture for user devanshi.srivastava
Written by devanshi.srivastava on 06/09/2021 - 19:52

In Python, there are many methods available on the list data type that help you remove an element from a given list. It have various in-built methods to remove items from the list.

1. Using del statement

The del statement is not a function of List. Items of the list can be deleted using del statement by specifying the index of item (element) to be deleted.

mylist = [1,2,3,4,5]
print("Before deleting the item: ", mylist)

del mylist[1]
print("After deleting the item: ", mylist)

Output: 

Before deleting the item:  [1, 2, 3, 4, 5]
After deleting the item:  [1, 3, 4, 5]

2. Using remove()

It is the built-in function ,it is used to remove the element .If the element is not present in the list it shows error. This function removes one element at a time from the given list. We can use iterator to remove range of elements.

myList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
print("Before removing the item: ",myList)

myList.remove(50)
print("After removing the item: ",myList)

Output: 

Before removing the item:  [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
After removing the item:  [10, 20, 30, 40, 60, 70, 80, 90, 100, 110, 120]

3. Using clear()

The clear() method will remove all the elements present in the list.

my_list = ['a','b','c','d','e']
print("Before clearing the items: ",my_list)

my_list.clear()
print("After clearing the items: ", my_list)

Output:

Before clearing the items:  ['a', 'b', 'c', 'd', 'e']
After clearing the items:  []

4. Using pop()

It can also be used to remove and return an element from the set, but by default it removes only the last element of the set, to remove element from a specific position of the List, index of the element is passed as an argument to the pop() method.

myList = [1,2,3,4,5,6]
print("Before pop operation: ",myList)

myList.pop()
print("After pop operation: ",myList)
myList.pop()
print("After using pop one more time: ",myList)

Output: 

Before pop operation:  [1, 2, 3, 4, 5, 6]
After pop operation:  [1, 2, 3, 4, 5]
After using pop one more time:  [1, 2, 3, 4]

Related Content
Python Tutorial
Python List
Python: Access List items
Tags
Python
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use