pythonarrayserase

Erase whole array Python


How do I erase a whole array, leaving it with no items?

I want to do this so I can store new values in it (a new set of 100 floats) and find the minimum.

Right now my program is reading the minimum from sets before I think because it is appending itself with the previous set still in there. I use .append by the way.


Solution

  • Note that list and array are different classes. You can do:

    del mylist[:]
    

    This will actually modify your existing list. David's answer creates a new list and assigns it to the same variable. Which you want depends on the situation (e.g. does any other variable have a reference to the same list?).

    Try:

    a = [1,2]
    b = a
    a = []
    

    and

    a = [1,2]
    b = a
    del a[:]
    

    Print a and b each time to see the difference.