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 it neatly fits in a

UPD Attack in Python

UDP flood attack A UDP flood attack is a denial-of-service (DoS) attack using the User Datagram Protocol (UDP), a sessionless/connectionless computer networking protocol. Using UDP for denial-of-service attacks is not as straightforward as with the Transmission Control Protocol (TCP). However, a UDP flood attack can be initiated by sending a large number of UDP packets to random ports on a remote host. As a result, the distant host will: Check for the application listening at that port; See that no application listens at that port; Reply with an ICMP Destination Unreachable packet. Thus, for a large number of UDP packets, the victimized system will be forced into sending many ICMP packets, eventually leading it to be unreachable by other clients. The attacker(s) may also spoof the IP address of the UDP packets, ensuring that the excessive ICMP return packets do not reach them, and anonymizing their network location(s). Most operating systems mit

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