Skip to main content

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])

To pop out lists
B=[1,[2,3]]
B.pop()
[2, 3]
will remove only one element or a list inside a list
so we iterate the list while the length og list becomes 0 and pop all elements out of our list
A [1,2,3,4,5,6]
while(len(A)>0):A.pop()

6
5
4
3
2
1

B=[1,[2,3],[4,5],6]
while(len(B)>0):B.pop()

6
[4, 5]
[2, 3]
1



3)Using del function
Delete function is used to permanently delete something from the memory.
If there is a list which we want to remove all elements using del function we write del then name of variable and slicing.
A=[1,2,3,4,5,6]
del A[::] #does slicing of all elements of a list
print(A)
[]
if we simply do a del function without slicing it removes that variable from memory space.
A=[1,2,3,4,5,6]
del A
print(A)
Traceback (most recent call last):
  File "<pyshell#176>", line 1, in <module>
    A
NameError: name 'A' is not defined

I know why would we delete the whole variable in the first place it can be used to free the memory and reallocate it to some other memory other than that we can slice the values however we want.

4) Remove
This function removes from a specific Array in the function.
Note - it only removes the first found occurance.

numbers = arr.array('i', [10, 11, 12, 12, 13])

numbers.remove(12)
print(numbers)

# Output: array('i', [10, 11, 12, 13])


Final thoughts
You can use any of the following methods to remove an element from a list or an array it depends on what logic you actually want to achieve from it.
Usually lists are much more flexible than arrays. They can store elements of different data types including string. Also, lists are faster than arrays. And, if you need to do mathematical computation on arrays and matrices, you are much better off using something like NumPy library like numpy arrays or Pandas library dataframes.

Comments

Popular posts from this blog

Columnar Transposition Cipher

Columnar Transposition Cipher Introduction  The columnar transposition cipher is a fairly simple, easy to implement cipher. It is a transposition cipher that follows a simple rule for mixing up the characters in the plaintext to form the ciphertext. Although weak on its own, it can be combined with other ciphers, such as a substitution cipher, the combination of which can be more difficult to break than either cipher on it's own. The  ADFGVX cipher uses a columnar transposition to greatly improve its security. Example  The key for the columnar transposition cipher is a keyword e.g.  GERMAN . The row length that is used is the same as the length of the keyword. To encrypt a piece of text, e.g. defend the east wall of the castle we write it out in a special way in a number of rows (the keyword here is  GERMAN ): G E R M A N d e f e n d t h e e a s t w a l l o f t h e c a s t l e x x In the above example, the plaintext has been padded so that ...

A Case Study On End-to-End Encryption Used In Whatsapp

A Case Study On End-to-End Encryption Used In Whatsapp 1.Introduction to End-to-End Encryption WhatsApp's end-to-end encryption is available when you and the people you message use the latest versions of the app. WhatsApp's end-to-end encryption ensures only you and the person you're communicating with can read what is sent, and nobody in between, not even WhatsApp. This is because your messages are secured with a lock, and only the recipient and you have the special key needed to unlock and read them. For added protection, every message you send has its own unique lock and key. WhatsApp, since its inception six years ago, has quickly grown into a global phenomenon, becoming one of the most popular mobile based communications applications in the world today. With a user base that eclipsed one billion in February, WhatsApp provides a service that potentially endangers the privacy of over 10% of the entire human population. In order to address these security concern...

Study of Support Vector Machines

Introduction to support vectors In machine learning, support vector machines (SVMs, also support vector networks) are supervised learning models with associated learning algorithms that analyze data used for classification and regression analysis. What are support vectors Support vectors are the data points that lie closest to the decision surface (or hyperplane) • They are the data points most difficult to classify • They have direct bearing on the optimum location of the decision surface • We can show that the optimal hyperplane stems from the function class with the lowest “capacity”= # of independent features/parameters Theoretical concept SVMs maximize the margin (Winston terminology: the ‘street’) around the separating hyperplane.  • The decision function is fully specified by a (usually very small) subset of training samples, the support vectors.  • This becomes a Quadratic programming problem that is easy to solve by standard methods Separation by Hyperplanes • Assu...