0

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.

  • 5
    What do you mean by clear it ? – Vincent Mar 23 '22 at 09:03
  • 2
    Maybe you want to initialize a list of lists `duplicates = [[]]*100` or a list of empty strings `["" for _ in range(100)]`? It is really unclear what the "array" is. – Mr. T Mar 23 '22 at 09:05
  • You have a size zero list and are attempting to access, a 1 through 100 position which doesnt exist, ofc it gives you index out of range. If you want to clear is simply redeclare is as an empty list, or if you want a 100 empty member, then do duplicates.append("") – Josip Juros Mar 23 '22 at 09:05
  • You may try to replace `duplicates[index].append("")`with `duplicates.append("")` in your code – gremur Mar 23 '22 at 09:05
  • Does this answer your question? [Erase whole array Python](https://stackoverflow.com/questions/3499233/erase-whole-array-python) – Amir Shamsi Mar 23 '22 at 09:24

4 Answers4

2

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()
1

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
Vamsi
  • 388
  • 2
  • 12
0

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("")
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
  • why did you change `range(100)` to `range(len(duplicates))` - as the list is empty the program will not get into the code in the `for` loop. –  Mar 23 '22 at 09:14
  • Yeah that was wrong, just re-read it. Fixed now. I also had the append method after an index too (which was a mistake), like `foo[0].append()` – Freddy Mcloughlan Mar 23 '22 at 09:15
0

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)
floupinette
  • 150
  • 11
  • Thank you, @floupinette ! But, does this mean that if my array needs to have 100 elements, I need to create it with 100 integers? – Albert van Zyl Mar 23 '22 at 09:36
  • In python, you can resize array : add new element (with 'append') or delete element using 'del' at any time. – floupinette Mar 23 '22 at 09:49