Home > OS >  How to clear an Array in Python?
How to clear an Array in Python?

Time:03-23

I am trying to create an array and then clear it using a function, but the terminal keeps displaying 'list index out of range'.

duplicates = []

def ClearArray():
    for index in range(100):
        duplicates[index].append("")

ClearArray()
print(duplicates)

The instruction is to Initialise the global array Duplicates, which is a 1D array of 100 elements of data type STRING, and set all elements to empty string using a function.

CodePudding user response:

You are getting the error because you are initializing an empty list and then trying to access elements inside of that list.

In the first iteration of the for loop, you attempt to access duplicates[0] but the list is empty, and therefore the list index out of range exception is raised. You can resolve this by replacing duplicates[index].append("") with duplicates.append("").

You can read more about python lists here.

In addition, according to python's PEP8 function names should be lowercase, with words separated by underscores as necessary to improve readability.

Putting it all together:

duplicates = []

def clear_array():
    for index in range(100):
        duplicates.append("")

clear_array()
print(duplicates)

To remove all elements of a list, you can use clear, like so:

duplicates.clear()

CodePudding user response:

I am not sure what you mean by clear, but when you create a list/array in python it will not have data

In case you need to add some data

duplicates = []
print(len(duplicates)) # length should be zero
for i in range(100):
  dupicates.append(i)

In case you need to clear all the data in the list

print(len(duplicates)) # Should print 100 from the above for loop    
duplicates.clear()
print(len(duplicates)) # Should be 0 again considering we have cleared the data

In case you are trying to create an array of arrays

duplicates = [[]] * 100
print(duplicates) # should show you 100 lists in a list

CodePudding user response:

You are trying to get something that doesn't exist.

Your list duplicates = [] has nothing in it. Therefore duplicates[0] does not exist, which is why you are getting list index out of range.

In your example:

def ClearArray():
    for index in range(100):
        duplicates[index].append("")

The first iteration of your for loop accesses duplicates[index] where index = 0, Which throws the error.


When you append, you add to the end. So there is no need to specify an index. So this below will work fine:

def ClearArray():
    for index in range(100):
        duplicates.append("")

CodePudding user response:

First create a non-empty array.

Then repeat deleting the first item of this array as many times as there are items in your array.

array = [43,54,65,76]

def ClearArray(array_to_clear):
    for index in range(len(array_to_clear)):
        del array_to_clear[0]

ClearArray(array)
print(array)

If you don't want to delete the items of the list but you want to replace each item with an empty string, then i recommend this code :

array = [43,54,65,76]

def ClearArray(array_to_clear):
    for index in range(len(array_to_clear)):
        array_to_clear[index] = ""

ClearArray(array)
print(array)
  • Related