Python List remove()

In this tutorial, we will see about Python List‘s remove method.Python List remove method is used to remove passed element from the list.It will remove first matching element from the list.

Python List remove syntax

here list1 is object of list.

Python List remove example

You can simply call remove method to delete an element from the list.
Let’s understand this with the help of simple example.

Output:

listOfCar: [‘Honda’, ‘Hyundai’, ‘Suzuki’]

As you can see here, ‘Maruti’ got removed from the list.
What if passed element is not in the list?
If element is not in the list, then remove method will raise ValueError.

Output:

—————————————————————————
ValueError Traceback (most recent call last)
in ()
1 listOfCars=[‘Honda’,’Hyundai’,’Maruti’,’Suzuki’] —-> 2 listOfCars.remove(‘Punto’)
3 print(“listOfCar:”,listOfCars)

ValueError: list.remove(x): x not in list

That’s all about Python List remove method. Also, check how to remove last element from list in python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63b391f3dc3b9238703513/] [crayon-63b391f3dc3bc722810700/] First, we created a […]

  • 27 November

    Remove Punctuation from List in Python

    Table of ContentsUse for loop with replace() MethodUse Nested List ComprehensionUse str.translate() Function Use for loop with replace() Method To remove punctuation from list in Python: Use the for loop to iterate over each word (element) of given list. Use a nested loop to iterate over each character in the word. Use the if statement […]

  • 04 October

    Get Every Other Element in List in Python

    Table of ContentsGet Every Other Element in List in PythonUsing List SlicingUsing the list.append() function along with the range() functionUsing list comprehension and the range() function with conditionalsUsing list comprehension and the range() function with stepUsing the enumerate() function along with list comprehensionUsing a lambda functionConclusion. A list is one of the more versatile data […]

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.Conclusion In this post, we will […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.