Python
Python Arrays: Create, Append, Pop, Reverse Examples
What is Python Array? A Python Array is a common type of data structure wherein all elements must...
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:
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',
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.
list.remove(element)
The element that you want to remove from the list.
ReturnValue
There is no return value for this method.
Following are the important points to remember when using remove () method:
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 <module> my_list.remove(100) ValueError: list.remove(x): x not in the list
The pop() method removes an element from the list based on the index given.
list.pop(index)
index: the pop() method has only one argument called index.
as -1.
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.
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 :
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 Tiya name = my_list.pop(2) print(name) print(my_list) #pop() method without index – returns the last element item = my_list.pop() print(item) print(my_list) #passing index out of range item = 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
The clear() method will remove all the elements present in the list.
list.clear()
Parameters:
No parameters.
ReturnValue:
Ther is no return value. The list() is emptied using clear() method.
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() method element = my_list.clear() print(element) print(my_list)
Output:
None []
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.
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:
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 firstelement del my_list[0] print("After removing first element", my_list) #To remove last element del my_list[-1] print("After removing last element", my_list) #To remove element for given index : for example index:5 del my_list[5] print("After removing element from index:5", my_list) #To remove last 2 elements from the list del my_list[-2] print("After removing last 2 elements", my_list) #To remove multiple elements delmy_list[1:5] print("After removing multiple elements from start:stop index (1:5)", my_list) #To remove multiple elements del 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]
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 element my_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 element element = 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 element my_list2 = ['A', 'B', 'C', 'D', 'E', 'F'] del my_list2[0] print("Using del, the final list is ", my_list2)
Output:
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 A Using pop(), the final list is ['B', 'C', 'D', 'E', 'F'] Using del, the final list is ['B', 'C', 'D', 'E', 'F']
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 list my_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']
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 list my_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 list my_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 C Using 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']
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
Method | Description |
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. |
What is Python Array? A Python Array is a common type of data structure wherein all elements must...
What is C++? C++ is widely used in general-purpose programming languages. The language allows you...
What is Python strip()? Python strip() function is a part of built-in functions available in the...
$20.20 $9.99 for today 4.5 (129 ratings) Key Highlights of Python Tutorial PDF 211+ pages eBook...
Python print() built-in function is used to print the given content inside the command prompt. The...
What are Logical Operators in Python? Logical Operators in Python are used to perform logical...