How do I remove a specific value from a list in Python?

Python List remove[]

In this tutorial, we will learn about the Python List remove[] method with the help of examples.

The remove[] method removes the first matching element [which is passed as an argument] from the list.

Example

# create a list prime_numbers = [2, 3, 5, 7, 9, 11]
# remove 9 from the list prime_numbers.remove[9]
# Updated prime_numbers List print['Updated List: ', prime_numbers] # Output: Updated List: [2, 3, 5, 7, 11]

Why use Lists?

Sometimes, there may be situations where you need to handle different types of data at the same time. For example, let’s say, you need to have a string type element, an integer and a floating-point number in the same collection. Now, this is fairly impossible with the default data types that are available in other programming languages like C & C++. In simple words, if you define an array of type integer, you can only store integers in it. This is where Python has an advantage. With its list collection data type, we can store elements of different data types as a single ordered collection!

Now that you know how important lists are, let’s move on to see what exactly are Lists and how to remove elements from list!

How to remove an item from the List in Python?

Python Lists have various in-built methods to remove items from the list. Apart from these, we can also use del statement to remove an element from the list by specifying a position. Let’s look at these methods –

Method 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.




# Python 3 code to
# remove an item from list
# using del statement
lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
'Lily', 'Carnations']
print["Original List is :", lst]
# using del statement
# to delete item [Orchids at index 1]
# from the list
del lst[1]
print["After deleting the item :", lst]

Output:

Original List is : [‘Iris’, ‘Orchids’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]
After deleting the item : [‘Iris’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]

Method 2: Using remove[]
We can remove an item from the list by passing the value of the item to be deleted as the parameter to remove[] function.






# Python 3 code to
# remove an item from list
# using function remove[]
lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
'Lily', 'Carnations']
print["Original List is :", lst]
# using remove[]
# to delete item ['Orchids']
# from the list
lst.remove['Orchids']
print["After deleting the item :", lst]

Output:

Original List is : [‘Iris’, ‘Orchids’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]
After deleting the item : [‘Iris’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]

Method 3: Using pop[]
pop[] is also a method of list. We can remove the element at the specified index and get the value of that element using pop[].




# Python 3 code to
# remove an item from list
# using function pop[]
lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
'Lily', 'Carnations']
print["Original List is :", lst]
# using pop[]
# to delete item ['Orchids' at index 1]
# from the list
a = lst.pop[1]
print["Item popped :", a]
print["After deleting the item :", lst]

Output –

Original List is : [‘Iris’, ‘Orchids’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]
Item popped : Orchids
After deleting the item : [‘Iris’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]




Article Tags :
Python
python-list
python-list-functions
Practice Tags :
python-list
Read Full Article

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

Python’s 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]
Let’s 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 doesn’t exists in list then list.remove[] will throw exception.
Therefore before calling list.remove[] we should either,
Advertisements

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

Python remove[] method

Python removes [] method is a built-in method available with the list. It helps to remove the given very first element matching from the list.

Syntax:

list.remove[element]

The element that you want to remove from the list.

ReturnValue

There is no return value for this method.

Tips for using remove[] method:

Following are the important points to remember when using remove [] method:

  • When the list has duplicate elements, the very first element that matches the given element will be removed from the list.
  • If the given element is not present in the list, it will throw an error saying the element is not in the list.
  • The remove [] method does not return any value.
  • The remove [] takes the value as an argument, so the value has to pass with the correct datatype.

Example: Using remove[] method to remove an element from the list

Here is a sample list that i have

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']

The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya.

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove[12] # it will remove the element 12 at the start. print[my_list] my_list.remove['Riya'] # will remove the first Riya from the list print[my_list] my_list.remove[100] #will throw an error print[my_list]

Output:

['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback [most recent calllast]: File "display.py", line 9, in my_list.remove[100] ValueError: list.remove[x]: x not in the list

Python - Remove List Items

❮ Previous Next ❯

Remove Specified Item

The remove[] method removes the specified item.

Example

Remove "banana":

thislist = ["apple", "banana", "cherry"]
thislist.remove["banana"]
print[thislist]
Try it Yourself »

Video liên quan

Bài mới nhất

Chủ Đề