Skip to main content

Posts

Showing posts with the label Python basics

How To Clear or Remove Elements From a list in Python

How To Clear or Remove Elements From a list in Python Sometimes When We add Elements in a list it becomes Troublesome to remove elemnts from the list so that we can use them again in our code or console. So to remove Elements from a list there are 3 ways 1)Re initialization of List 2)Using Pop function 3)Using del function 4)Using Remove function 1)Re initialization Reintialing the list clears up the whole list for reuse of the elements or variables so if my list is full of elements eg. A=[1,2,3,4,5,6] and i want A as an empty list we simply re initialize a list A=[] gives us an Empty list which clears up the whole variable 2)Pop function It removes the last element if unspecified or removes the specified indexed element from a list or array so if my list is full of elements eg. A=[1,2,3,4,5,6] A.pop() 6 numbers = arr.array('i', [10, 11, 12, 12, 13]) print(numbers.pop(2)) # Output: 12 print(numbers) # Output: array('i', [10, 11, 13]) T