How do you delete a certain item from a list?

Remove an item from a list in Python [clear, pop, remove, del]

Posted: 2019-05-29 / Modified: 2021-04-06 / Tags: Python, List
Tweet

In Python, use list methods clear[], pop[], and remove[] to remove items [elements] from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

  • Remove all items: clear[]
  • Remove an item by index and get its value: pop[]
  • Remove an item by value: remove[]
  • Remove items by index or slice: del
  • Remove items that meet the condition: List comprehensions

See the following article for adding items to the list.

  • Add an item to a list in Python [append, extend, insert]
Sponsored Link

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!

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]

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 »

How to remove item from list python?

In this python tutorial, we look at all the different methods you could use to remove item from list in python. We also break down which methods are best suited for each use case.

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

Video liên quan

Bài mới nhất

Chủ Đề