How to remove an item from the List in Python?

Python Lists have different in-constructed strategies to eliminate things from the rundown. Aside from these, we can likewise utilize del articulation to eliminate a component from the rundown by determining a position. We should check out these techniques.

Python List information type assists you with putting away things of various information types in an arranged grouping. The information is composed inside square sections ([]), and the qualities are isolated by comma(,).

In Python, there are numerous strategies accessible on the rundown information type that assist you with eliminating a component from a given rundown. The strategies are eliminate(), pop() and clear() .
Eliminating things from the list – Python

Since records are alterable, python comes worked with a couple of rundown techniques that can be utilized to eliminate things from records. A portion of these techniques are pop(), eliminate(), clear(), and so on

Albeit this multitude of techniques can be utilized to eliminate things from records in python they are worked to take into account diverse use-cases, we talk about them exhaustively beneath.

Aside from these rundown techniques, the del() strategy can likewise be utilized to eliminate things in a rundown.

Syntax of remove():

list.remove(value)

Here list refers to the name of the list.

Parameters:

value – The element that you want to remove.

Code to remove item from list by value:

list1 = ["Hire","Hire", "the", "top", 10, "python","freelancers"]

list1.remove("Hire")
print(list1)

#Output - ['Hire', 'the', 'top', 10, 'python', 'freelancers']

Eliminate thing from list by Index:

Since records are requested, every thing can be alluded to utilizing its file. These files allude to an interesting worth and henceforth it tends to be utilized to eliminate things from a rundown in python.

For this, we utilize the pop() techniques. Another utilization instance of pop() is to eliminate and return the erased thing. In such cases pop() is utilized rather than eliminate(). Additionally, note that assuming no record is passed as a boundary, pop() eliminates and returns the last thing.

Syntax of pop()

list.pop(index)

Here list refers to the name of the list.

Parameters:

index – Optional, the index of the item you want to remove.

Returns:

Return the item removed from the list, in case you do not pass an index the last value is returned.

Code to remove item from list by index:

list1 = ["Hire", "the", "top", 10, "python","freelancers"]

removed_item = list1.pop(0)
print(list1)
print(removed_item)

#Output - ['the', 'top', 10, 'python', 'freelancers']
#Output - "Hire"

Utilizing Value to eliminate things

In this technique, we eliminate things from the rundown dependent on their worth. We accomplish this by utilizing the eliminate() list technique. This technique is very direct, notwithstanding, it is case touchy and just eliminates the primary event of the worth.

# 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’]

Also Read: Iterate Over the Characters of a String in Java

Leave a Reply

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