python provides operator for deleting/removing an item from a list. There are many methods for deletion:
The pop() method removes and returns last object or obj from the list.
Following is the syntax for pop() method −
list.pop(obj = list[-1])
obj − This is an optional parameter, index of the object to be removed from the list.
This method returns the removed object from the list.
The following example shows the usage of pop() method.
list 1 = ['physics', 'Biology', 'chemistry', 'maths']
list 1.pop()
print ("list now : ", list 1)
list 1.pop(1)
print ("list now : ", list 1)
output:
list now : ['physics', 'Biology', 'chemistry']
list now : ['physics', 'chemistry']
The del statement can be used to remove an item from a list by referring to its index, rather than its value. For example, if you have a list with five values, like this:
a = [1, 2, 3, 4, 5]
and you want to remove the second listed value (2), you can use del to do so. The second listed value has an index of 1, so to remove the value, the del syntax would need to look like this:
del a[1]
Now a will look like this:
a = [1, 3, 4, 5]
remove() is an inbuilt function in Python programming language that removes a given object from the list. It does not return any value.
list_name.remove(obj)
obj - object to be removed from the list
The method does not return any value
but removes the given object from the list.
Note:It removes the first occurrence of the object from the list.
# Python 3 program to demonstrate the use of
# remove() method
# the first occurrence of 1 is removed from the list
list 1 =
[ 1, 2, 1, 1, 4, 5
]
list 1.remove(1)
print(list 1)
# removes 'a' from list 2
list 2 =
[ 'a', 'b', 'c', 'd'
]
list 2.remove('a')
print(list 2)
output:
[2, 1, 1, 4, 5]
['b', 'c', 'd']
Python list method max returns the elements from the list with maximum value.
Following is the syntax for max() method −
max(list)
This method returns the elements from the list with maximum value.
list 1, list 2 = [123, 'xyz', 'zara', 'abc'], [456, 700, 200]
print "Max value element : ", max(list 1)
print "Max value element : ", max(list 2)
output:
Max value element : zara
Max value element : 700
Python list method min() returns the elements from the list with minimum value.
Following is the syntax for min() method −
min(list)
This method returns the elements from the list with minimum value.
list 1, list 2 = [123, 'xyz', 'zara', 'abc'], [456, 700, 200]
print "min value element : ", min(list 1)
print "min value element : ", min(list 2)
output:
min value element : 123
min value element : 200