HomeLinuxPython Clear Record

Python Clear Record


The “Lists” are ordered collections of things that may be modified and accessed by index. These are helpful for storing and manipulating knowledge, resembling numbers, strings, objects, or different lists. Nevertheless, typically we could have to clear a listing, which implies eradicating all of the gadgets from it, thereby making it empty. This may be achieved for varied causes, resembling releasing up reminiscence, resetting the listing for a brand new use case, or avoiding undesirable unwanted effects.

This weblog will discover other ways to clear a listing in Python using related examples.

The best way to Clear Record in Python?

Python offers a number of strategies for clearing a listing, together with the next:

Technique 1: Clear Record in Python Utilizing “clear()” Technique

In Python, the “clear()” methodology is utilized to take away/delete all the weather/gadgets from the actual listing.

Syntax

There isn’t any return worth from this methodology, nor does it take any parameter worth.

Instance

The next code is utilized to clear all the weather of the listing:

list1 = [18, 15, 10, 22, 32]

print(‘Given Record: ‘, list1)

list1.clear()

print(‘After Clearing: ‘, list1)

Within the above code, the “listing.clear()” methodology is utilized to clear all of the gadgets of the initialized listing.

Output

All of the listing parts have been cleared efficiently.

Technique 2: Clear Record in Python Utilizing the “del” Key phrase

In Python, the “del” key phrase is utilized to delete/take away objects resembling lists, strings, dictionaries, and so on.

Instance

Right here is an instance code that’s utilized to clear/take away the listing:

list1 = [18, 15, 10, 22, 32]

print(‘Given Record: ‘, list1)

del list1[:]

print(‘After Clearing: ‘, list1)

Within the above code strains, the “del” key phrase is utilized together with the slice notation “ list1[:]” to delete all the weather of the given listing. Right here the “Slice Notation” means to pick out all of the gadgets from the beginning until the tip of the listing.

Output

As seen, all of the listing gadgets have been cleared efficiently.

Technique 3: Clear Record in Python Utilizing “pop()” Technique With “whereas” Loop

In Python, the “pop()” methodology is used to take away/delete a specific worth from the listing. Right here, this methodology is used mixed with the “whereas” loop to clear the listing in Python.

Syntax

Right here the “index” parameter signifies the place of factor/gadgets.

Instance

The beneath code is used to clear all of the listing parts:

list1 = [18, 15, 10, 22, 32]

print(‘Given Record: ‘, list1)

whereas(len(list1) != 0):

list1.pop()

print(‘After Clearing: ‘, list1)

Based on the above code, the “whereas” loop is used with the “len()” perform to examine whether or not the outlined listing size is zero or not. If the listing size is non-zero, the related “pop()” methodology removes the weather from the listing until it, i.e., listing size equals zero.

Output

Technique 4: Clear Record in Python Utilizing “take away()” Technique

The “take away()” methodology can be used to clear the listing by taking the desired factor as an argument.

Syntax

Instance

Within the beneath code, the “listing.take away()” methodology is used to clear the desired factor from the listing:

list1 = [18, 15, 10, 22, 32]

print(‘Given Record: ‘, list1)

list1.take away(10)

print(‘After Clearing: ‘, list1)

On this code block, the “take away()” methodology takes/accepts the desired listing merchandise as an argument and eliminates it from the given listing.

Output

Right here, it may be implied that the desired factor from the listing is eliminated as an alternative.

Be aware: This methodology can’t take away all the weather from the listing directly.

Conclusion

The “clear()” methodology, “del” key phrase, “pop()” methodology with “whereas” loop, or the “take away()” methodology are used to clear the listing in Python. The “clear()” methodology empties all of the listing parts and returns an empty listing, equally the “del” key phrase is used with slicing notation to clear all of the listing gadgets in Python. The opposite strategies may also effectively clear all of the listing gadgets. These strategies are explored on this Python weblog to successfully clear the listing utilizing a number of examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments