How to Clear an Array in Python?

Estimated read time 1 min read

In Python, you can clear an array by resetting it to an empty list. Here’s an example:

my_array = [1, 2, 3, 4, 5]
my_array = []

In the above code, the my_array variable is assigned to an empty list, effectively clearing its contents.

Alternatively, you can also use the clear() method to clear an array. Here’s an example:

my_array = [1, 2, 3, 4, 5]
my_array.clear()

In the above code, the clear() method removes all elements from the my_array list, effectively clearing its contents.

Note that if you have multiple references to the same array object, resetting the variable to an empty list or calling the clear() method will only clear the contents of that specific reference. If you want to clear all references to the array object, you’ll need to make sure that all references are reset or cleared.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply