#remove specific element in python list
list=list([34,55,34,66,77,45.6,23,34,])
print(list)
list.remove(66)
print(list)
output:
[34, 55, 34, 66, 77, 45.6, 23, 34]
[34, 55, 34, 77, 45.6, 23, 34]
#Remove all occurrence of a specific python repeated element
mylist = list([26, 4, 26, 26, 8, 12])
for item in mylist:
mylist.remove(26)
print(mylist)
output :
[4,8,12]
#remove item present at given index
mylist = list([2, 4, 6, 8, 10, 12])
mylist.pop(2) #remove 2 index based element
print(mylist)
output:
[2, 4, 8, 10, 12]
# remove item without passing index number(last value remove) using pop()
mylist = list([2, 4, 6, 8, 10, 12])
mylist.pop()
print(mylist)
output :
[2, 4, 6, 8, 10]
[2, 4, 8, 10]
#remove the range of elements python list with slice:
mylist = list([2, 4, 6, 8, 10, 12])
#remove item from index 2 to 5
del mylist[2:5]
print(mylist)
output:
[2, 4, 12]
#remove all items starting from index 2
mylist=list([23,44,55,34,53,78,55])
del mylist[2:]
print(mylist)
output:
[23,44]
#remove entair list using del keyword
mylist=list([23,44,55,34,53,78,55])
del mylist
print(mylist)
output:
mylist is not found
Comments
Post a Comment