Remove from list Python

In this post, we will see how to remove an element from list in python.

You can remove elements from list in 3 ways.

Table of Contents


Using list object’s remove method

Here you need to specify element which you want to remove. If there are multiple occurrences of element, then first occurrence will be removed.

Please note that this is quite slow as it searches for element in the list and then remove the element by its value.
If the element is not present in the list, then it raises ValueError.

[‘India’, 1, ‘Nepal’, 5, 1, 2, 3, 4, 3, 2] —————————————————————————
ValueError Traceback (most recent call last)
in ()
3 print(list1)
4
—-> 5 list1.remove(9)
6 print(list1)

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


Using list object’s pop method

This method takes index of element as input and remove it. It throws IndexError if index is out of range.

[‘India’, 1, 2, 5, 1, 2, 3, 4, 3, 2] —————————————————————————
IndexError Traceback (most recent call last)
in ()
3 print(list1)
4
—-> 5 list1.pop(14)
6 print(list1)

IndexError: pop index out of range


Using operator del

This operator takes index of element as a input and remove the element. del operator also supports removing range of elements from the list.This is clear and fast way of removing elements from the list.

[‘India’, 1, 2, 5, 1, ‘France’, 2, 3, 4, 3, 2] [‘India’, 1, 2, 5, 3, 4, 3, 2]

That’s all about Python remove from list.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.