Remove element from a Python LIST [clear, pop, remove, del] (2023)

Python List data-type helps you to store items of different data types in an ordered sequence. The data is written inside square brackets ([]), and the values are separated by comma(,).

In Python, there are many methods available on the list data type that help you remove an element from a given list. The methods are remove(), pop() and clear() .

Besides the list methods, you can also use a del keyword to remove items from a list.

In this Python tutorial, you will learn:

  • Python remove() method
  • Python pop() method
  • Python clear() method
  • Using del keyword
  • How do I remove the first element from a list?
  • How do I remove multiple elements from a list in Python?
  • How do I remove an element from a list by using an index in Python?

Example of list

my_list = ['Guru', 50, 11.50, 'Siya', 50, ['A', 'B', 'C']]

The index starts from 0. In the list: my_list at

0th index we have the string ‘Guru’,

  • At index: 1 you will get the number 50 which is a integer.
  • At index:2 you will get the floating number 11.50
  • At index:3, there is a string ‘Siya.’
  • At index:4, you will see the number 50 is duplicated.
  • At index:5, you will get a list with values A, B, and C.

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.

(Video) Remove Items from a List in Python (Pop, Del, Remove, Clear) - Python Tutorial for Beginners

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 listprint(my_list)my_list.remove(100) #will throw an errorprint(my_list)

Output:

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

Python pop() method

The pop() method removes an element from the list based on the index given.

Syntax

list.pop(index)

index: the pop() method has only one argument called index.

  • To remove an element from the list, you need to pass the index of the element. The index starts at 0. To get the first element from the list pass index as 0. To remove the last element, you can pass the indexas -1.
  • The index argument is optional. If not passed, the default value is considered -1, and the last element from the list is returned.
  • If the index given is not present, or out of range, the pop() method throws an error saying IndexError: pop index.

ReturnValue:

The pop() method will return the element removed based on the index given. The final list is also updated and will not have the element.

(Video) [3 Ways] How to remove element/item from Python list? Difference Between remove, del and pop?

Example: Using the pop() method to remove an element from the list

The list will use in the example is my_list = [12, ‘Siya’, ‘Tiya’, 14, ‘Riya’, 12, ‘Riya’] .

Let us try to remove element using a pop() method based on the following :

  • By giving index
  • Without index
  • Passing index that is out of range.

Here, we are removing Tiya from the list. The index starts from 0 , so the index for Tiya is 2.

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']#By passing index as 2 to remove Tiyaname = my_list.pop(2)print(name)print(my_list)#pop() method without index – returns the last elementitem = my_list.pop()print(item)print(my_list)#passing index out of rangeitem = my_list.pop(15)print(item)print(my_list)

Output:

Tiya[12, 'Siya', 14, 'Riya', 12, 'Riya']Riya[12, 'Siya', 14, 'Riya', 12]Traceback (most recent calllast):File "display.py", line 14, in <module>item = my_list.pop(15)IndexError: popindex out of range

Python clear() method

The clear() method will remove all the elements present in the list.

Syntax:

list.clear()

Parameters:

No parameters.

ReturnValue:

Ther is no return value. The list() is emptied using clear() method.

(Video) How to remove items through a list | Python programming | clear(), pop(), del, remove()

Example: Using clear() method to remove all elements from the list

The clear() method will empty the given list. Let us see the working of clear() in the example below:

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']#Using clear() methodelement = my_list.clear()print(element)print(my_list)

Output:

None[]

Using del keyword

To remove an element from the list, you can use the del keyword followed by a list. You have to pass the index of the element to the list. The index starts at 0.

Syntax:

del list[index]

You can also slice a range of elements from the list using the del keyword. The start/stop index from the list can be given to del keyword, and the elements falling in that range will be removed. The syntax is as follows:

Syntax:

del list[start:stop]

Here is an example that shows to remove the first element, last element, multiple elements from the list using del.

my_list = list(range(15))print("The Original list is ", my_list)#To remove the firstelementdel my_list[0]print("After removing first element", my_list)#To remove last elementdel my_list[-1]print("After removing last element", my_list)#To remove element for given index : for example index:5del my_list[5]print("After removing element from index:5", my_list)#To remove last 2 elements from the listdel my_list[-2]print("After removing last 2 elements", my_list)#To remove multiple elementsdelmy_list[1:5]print("After removing multiple elements from start:stop index (1:5)", my_list)#To remove multiple elementsdel my_list[4:]print("To remove elements from index 4 till the end (4:)", my_list)

Output:

The Originallist is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]After removing first element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]After removing last element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]After removing element from index:5 [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13]After removing last 2 elements [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13]After removing multiple elements from start:stop index (1:5) [1, 7, 8, 9, 10, 11, 13]To remove elements from index 4 till the end (4:) [1, 7, 8, 9]

How do I remove the first element from a list?

You can make use of list methods like remove(), pop() to remove the first element from the list. In the case of remove() method, you will have to pass the first element to be removed and for pop the index, i.e., 0.

You may also use the del keyword to remove the first element from the list.

The example below shows to remove first element from list using remove(), pop() and del.

my_list1 = ['A', 'B', 'C', 'D', 'E', 'F']print("The Originallist is ", my_list1)#Using remove() to remove first elementmy_list1.remove('A')print("Using remove(), the final list is ", my_list1)my_list1 = ['A', 'B', 'C', 'D', 'E', 'F']print("The Originallist is ", my_list1)#Using pop() to remove the first elementelement = my_list1.pop(0)print("The first element removed from my_list1 is ", element)print("Using pop(), the final list is ", my_list1)#Using del to remove the first elementmy_list2 = ['A', 'B', 'C', 'D', 'E', 'F']del my_list2[0]print("Using del, the final list is ", my_list2)

Output:

(Video) 8. remove(), pop(), del, clear() functions List | Python Lectures |

The Originallist is ['A', 'B', 'C', 'D', 'E', 'F']Using remove(), the final list is ['B', 'C', 'D', 'E', 'F']The Originallist is ['A', 'B', 'C', 'D', 'E', 'F']The first element removed from my_list1 is AUsing pop(), the final list is ['B', 'C', 'D', 'E', 'F']Using del, the final list is ['B', 'C', 'D', 'E', 'F']

How do I remove multiple elements from a list in Python?

The list methods remove(), and pop() are meant to remove a single element. To remove multiple aspects, make use of the del keyword.

From the list [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’], we want to remove elements B, C and D. Below example shows the how to make use of del keyword to remove the elements.

#Using del to remove the multiple elements from listmy_list2 = ['A', 'B', 'C', 'D', 'E', 'F']print("Originallist is ", my_list2)del my_list2[1:4]print("Using del, the final list is ", my_list2)

Output:

Originallist is ['A', 'B', 'C', 'D', 'E', 'F']Using del, the final list is ['A', 'E', 'F']

How do I remove an element from a list by using index in Python?

To remove element based on index, you can make use of list method pop() . Even using del keyword will help you to remove the element for a given index.

#Using del to remove the multiple elements from listmy_list1 = ['A', 'B', 'C', 'D', 'E', 'F']print("Originallist is ", my_list1)element = my_list1.pop(2)print("Element removed for index: 2 is ", element)print("Using pop, the final list is ", my_list1)#Using del to remove the multiple elements from listmy_list2 = ['A', 'B', 'C', 'D', 'E', 'F']print("Originallist is ", my_list2)del my_list2[2]print("Using del, the final list is ", my_list2)

Output

Originallist is ['A', 'B', 'C', 'D', 'E', 'F']Element removed for index: 2 is CUsing pop, the final list is ['A', 'B', 'D', 'E', 'F']Originallist is ['A', 'B', 'C', 'D', 'E', 'F']Using del, the final list is ['A', 'B', 'D', 'E', 'F']

Summary:

In Python, there are many methods available on the list data type that helps you to remove an element from a given list. The methods are remove(), pop() and clear().

Important built-in methods available on list to remove elements

MethodDescription
remove()It helps to remove the very first given element matching from the list.
pop()The pop() method removes an element from the list based on the index given.
clear()The clear() method will remove all the elements present in the list.

You Might Like:

  • PyQt5 Tutorial with Examples: Design GUI using PyQt in Python
  • How to Find Average of a List in Python
  • 15 BEST Python Courses Online for Beginners (2023 Update)
  • Python Program to Find the Factorial of a Number

FAQs

What is the difference between Del () and remove () methods of list? ›

Remove() deletes the matching element from the list whereas the del and pop removes the element present at specified index.

What's the difference between del and remove in Python? ›

The del deletes an element at a specified index number. It removes the first matching value from the Python List. The del is simple deletion. The remove() searches the list to find the item.

What does Del () do in Python? ›

The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.

What method is used to remove elements from a set Clear () delete () discard () Drop ()? ›

To remove an item in a set, use the remove() , or the discard() method.

Is Del or remove faster in Python? ›

You can bet that del is faster. remove() has to find the element, and then delete it, practically with del . If you are removing elements from a list so frequently that the difference would be noticeable, you should probably be using a different data structure anyway.

What is the difference between remove () and pop () in list explain with example? ›

The major difference between the pop() method and the remove() method is that the pop() method uses the index of an element to delete it while the remove() method takes the value of the element as an input argument to delete the element as we have already seen above.

How do I remove an element from a list? ›

There are three ways in which you can Remove elements from List:
  1. Using the remove() method.
  2. Using the list object's pop() method.
  3. Using the del operator.
Jan 3, 2023

How do you remove items from a list in Python? ›

How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.

How is the pop () method different from the remove () method of a Python list? ›

Array elements can be removed using pop() or remove() method. The difference between these two functions is that the former returns the deleted value whereas the latter does not. The pop() function takes either no parameter or the index value as its parameter.

Is Del the same as delete? ›

Del is a common abbreviation for delete and a key on a computer keyboard. It is sometimes used with other keys to perform certain actions on a computer, also known as shortcut keys.

When should I use Del? ›

The preposition de is translated as "of," "from," or "about," but de also can mean "by," "in," or other prepositions in some cases. Del is simply the contraction of de and the definite article el (not él), so we use del in place of de el.

What is Python __ del __? ›

In Python, the __del__() method is referred to as a destructor method. It is called after an object's garbage collection occurs, which happens after all references to the item have been destroyed.

What is the difference between remove () and discard () functions in set? ›

The discard() method removes the specified item from the set. This method is different from the remove() method, because the remove() method will raise an error if the specified item does not exist, and the discard() method will not.

What is remove () method? ›

The remove() method removes the first occurrence of the element with the specified value.

What are the two methods for removing items from a list? ›

The methods are remove(), pop() and clear(). It helps to remove the very first given element matching from the list. The pop() method removes an element from the list based on the index given. The clear() method will remove all the elements present in the list.

Should I use del in Python? ›

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc.

What can I use instead of remove in Python? ›

For this, we use the pop() methods. Another use case of pop() is to remove and return the deleted item. In such cases pop() is used instead of remove() .

What is the purpose of del operator and pop method? ›

In python del is a keyword and remove(), pop() are in-built methods. The purpose of these three are same but the behavior is different remove() method delete values or object from the list using value and del and pop() deletes values or object from the list using an index.

What are the 3 ways of deleting an element from a list Explain with examples? ›

For simplicity, let's assume that the list used is l = [2, 3, 4, 5] .
  • Clear. The clear method removes all elements from the list. ...
  • Pop. The pop method removes the list element at a certain index and returns the value of the element once it's removed. ...
  • Remove. ...
  • Del.
Jan 31, 2020

Does pop remove the item from the list? ›

The pop() method removes the item at the given index from the list and returns the removed item.

How is pop () different from Popitem () and Del explain with examples? ›

Although we use pop() and popitem() to remove elements from a dictionary, they are actually different. pop() can remove any item from a dictionary as long as you specify the key. On the other hand, popitem() can only remove and return the value of the last element in the dictionary.

How do I remove an element from a list in Python by index? ›

You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output.

How do you remove an element from a list without function in Python? ›

Delete an element from a list using the del. The Python del statement is not a function of List. Items of the list can be deleted using the del statement by specifying the index of the item (element) to be deleted.

How do I remove a specific element from an array list? ›

There are 3 ways to remove an element from ArrayList as listed which later on will be revealed as follows:
  1. Using remove() method by indexes(default)
  2. Using remove() method by values.
  3. Using remove() method over iterators.
Jul 7, 2022

What list pop () and list remove () command does for list operation? ›

Use del to remove an element by index, pop() to remove it by index if you need the returned value, and remove() to delete an element by value. The last requires searching the list, and raises ValueError if no such value occurs in the list.

What is the difference between drop and pop? ›

drop returns the removed column while DataFrame. pop doesn't. Another difference is that pop can be used for both columns and rows.

How Pop () works in Python? ›

List pop in Python is a pre-defined, in-built function that removes an item at the specified index from the list. You can also use pop in Python without mentioning the index value. In such cases, the pop() function will remove the last element of the list.

What is the difference between calling a list's remove method and using the del statement to remove an element? ›

What is the difference between calling a list's remove method and using the del statement to remove an element? the remove method is used to remove a specific element in a list. the del method is used to remove any element that is present at a specific index, regardless of what that element is.

Is Remove from list the same and delete? ›

Remove from list is for removing follow up flags from flagged email messages. If you delete the flagged message from tasks, you delete the flagged message from your mailbox. Was this reply helpful?

What does list remove () do in Python? ›

Python List remove() is an inbuilt function in the Python programming language that removes a given object from the List.

Videos

1. How to delete an element from the list in python| for beginners|del |remove()|pop()
(Cloud-Devops)
2. Python functions - remove, pop, del, clear
(Exam Board)
3. Python Programming 19 - Remove Elements From List within for Loop
(Caleb Curry)
4. Remove Items from Set in Python? (Pop, Remove, Discard, Clear, Del) - Python Tutorial for Beginners
(Digital Academy)
5. Python list delete data from list | Pop(),Del(),Remove(), Method | Python for beginners | Prozcore
(Proz Core)
6. Python Tutorial - 17 - Lists - Delete, Remove, Pop Elements #webdevpro
(WebDevPro)
Top Articles
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated: 11/14/2022

Views: 5877

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.