EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python Lists Tutorial Python list remove()
 

Python list remove()

Priya Pedamkar
Article byPriya Pedamkar

Python list remove()

Overview of Python list remove()

List in python is a pre-defined method used for performing operations on a group/ collection of items. Though it is a similar construct to an array, the list function is heterogeneous, unlike the array function, contributing to its powerful accessibility in python. This list function allows many operations like insert, append, sum, count, extend, index, etc. One such operation is remove(), which is used for removing or deleting an item from the list.

 

 

List

The list is a similar concept to arrays. However, the only difference between both is that the array is always homogeneous while the list can be heterogeneous. This feature of the list makes it stand powerful in Python.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Example:

List_example = ['Mathematics', 'biology', 1997,'Hello123'] 
print(List_example)

Output:

python list remove()

The list always starts with index 0. Each element in the list holds its space. The list can have duplicate values in it, as the index would be different for both. The list has many methods to perform various operations related to it. Like:

Insert, extend, append, sum, count, index, remove, etc.

Let us discuss one important method here:

remove(): The method remove() is to delete elements from a list.

Syntax:

remove()

Examples of Python list remove()

Let’s get the concept clear through some examples:

Example #1

Code:

List_example = [2.34, 2.3, 3.4, 4.1, 1.054]
List_example.remove(3.4)
print(List_example)

Output:

python list remove()

As one can notice, we defined a list and then, with the help of the method, remove() we removed element 3.4 from the list. The element that needs to be removed has to go as an argument to those methods. Here we removed only one element. However, one must be wondering: what if we need to remove more than one element.

In that case, remove() doesn’t work in one go. The other way of doing it could be:

Code:

List_example = [2.34, 2.3, 3.4, 4.1, 1.054,6,7,8]
List_example.remove(6)
List_example.remove(7)
List_example.remove(8)
print(List_example)

Output:

python list remove()

If there are few elements to be removed from the list, it could be done one by one(as shown above). But if it’s more elements, then this way of doing it could be tedious and unprofessional.

Code:

item_list_ex = ['dog', 5, 'cat', 8.9, True]
print(item_list_ex)
item_list_ex = [x for x in item_list_ex if x not in ('dog', 5)]
print(item_list_ex)

Output:

python list remove()

This way is best to remove elements, as you need to mention all elements to be removed in one shot(as shown in the above logic).

Example #2

Let’s see an example with try and catch block.

Code:

a = ['Heena', 10, 'Rohit', 80, True]
try:
    a.remove(90)
except ValueError:
    print("Doesn't exist")
    pass  # do nothing!

Output:

python list remove()

As one can notice, element 90 is not part of the list. Hence, while executing a.remove(90), it has thrown ValueError. Henceforth, “Doesn’t exist” gets printed.

Example #3

If there is a sequence of numbers, and you want to remove the range of elements from it. It can be done with the help of for loop. See the example shown below:

Code:

List_example = [1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12]
print("Present List: ") 
print(List_example)
for i in range(5, 8): 
    print(i)
    List_example.remove(i) 
print("\nList after Removing a range of elements: ") 
print(List_example)

Output:

Present List

One can see, from a list of 1 to 12, three elements have been removed with the help of range, for loop and remove function.

A list can hold duplicate values, so how does it remove() work in that case? Let’s explore the answer to that.

Example #4

Code:

List_example = [2.34, 2.3, 3.4, 4.1, 1.054,6,2.3,7,8,2.3]
print("Present List: ",List_example)
List_example.remove(2.3)
print("Present List: ",List_example)

Output:

List Example

Method remove() only removes the first matching element from the list. As shown in the example, there are three values of 2.3. When we removed it, two still the same available on the list.

Note: One major challenge is that people usually get confused over remove() and pop()?

The simple answer would be, remove() is a value-based method and hence can remove the element from any place of the list by specifying its value. It could first, last or in between. However, the pop() method removes the last element(that’s by default). But if elements need to be removed from any position, its index has to be specified. In other words, pop() is an index-based removal of elements from the list.

Example #5

Highlighting difference between remove() and pop():

Code:

List_example = [1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12]
print("Present List: ",List_example)
List_example.remove(3)
print("Present List: ",List_example)
List_example.pop(3)
print("Present List: ",List_example)

Output:

difference between remove() and pop()

As one can notice, remove(3) removed value 3 from the list. However, pop(3) removed the element at the 3rd position, which is 5. The list always starts from index 0; hence 3rd element of a list is value 5.

The advantage of using a list lies in the fact that one can easily iterate over it. as the sequence of elements is easy to handle here.

Conclusion – Python list remove()

The above covered is one of the most important and basic methods which is widely used while handling the list in python. Then we saw the difference between pop and remove, which is a usual confusing parameter while handling the list. This topic has given enough highlights about the list remove(). One can go through other methods of the list to get well versed with the list concept. Don’t forget, Collection is one of the widely used concepts in python. The more one knows about the methods, the handier it becomes to write and understand the python code.

Recommended Articles

This is a guide to the Python list remove(). Here we discuss the overview, Examples of Python list remove(), and the codes & outputs. You may also look at the following articles to learn more –

  1. List Method in Python
  2. Python List Comprehension
  3. Send Mail in Python
  4. Python Empty List

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW