46

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.

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
pjehyun
  • 911
  • 2
  • 9
  • 11

5 Answers5

59

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.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • Really, there are arrays in Python? I did not know that. (Actually I probably knew but forgot) Anyway +1 for expanding on the difference between our answers. I just made a guess that the difference wouldn't matter for the OP. – David Z Aug 17 '10 at 04:12
  • i see. that helps @david: yah, there's not much difference for me. but my newbness has gotten lessened :P – pjehyun Aug 17 '10 at 04:13
  • "arrays are different": not really, `del` works; see my answer – John Machin Aug 17 '10 at 05:34
  • @John, I didn't mean `del` wouldn't work, just that lists and arrays are different. – Matthew Flaschen Aug 17 '10 at 05:45
35

It's simple:

array = []

will set array to be an empty list. (They're called lists in Python, by the way, not arrays)

If that doesn't work for you, edit your question to include a code sample that demonstrates your problem.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • This is how you re-initialize array if you are doing in a loop. This is fundamentally the right answer to the OP's question. – Rene Duchamp Jan 29 '19 at 15:43
  • I prefer this approach as coming from a VBA background we use `array = ("")` so very similar and easy to rememeber! +1 – Dean Dec 18 '19 at 19:14
9

Well yes arrays do exist, and no they're not different to lists when it comes to things like del and append:

>>> from array import array
>>> foo = array('i', range(5))
>>> foo
array('i', [0, 1, 2, 3, 4])
>>> del foo[:]
>>> foo
array('i')
>>> foo.append(42)
>>> foo
array('i', [42])
>>>

Differences worth noting: you need to specify the type when creating the array, and you save storage at the expense of extra time converting between the C type and the Python type when you do arr[i] = expression or arr.append(expression), and lvalue = arr[i]

John Machin
  • 81,303
  • 11
  • 141
  • 189
  • 1
    Far too few answers know the difference between a python `array` type and a python list. +1 for you. – Kevin Anderson Dec 12 '18 at 13:47
  • It is subtle, but it is important to say that if the list is in a hierarchy of objects, in certain cases we will lose its reference if we simply do `foo = []`, as we are defining a new list for "foo". For this reason an "up" for `del foo[:]`, because in this case we are modifying the originbal "foo" list. Thanks! =D – Eduardo Lucio Mar 20 '20 at 18:37
1

Now to answer the question that perhaps you should have asked, like "I'm getting 100 floats form somewhere; do I need to put them in an array or list before I find the minimum?"

Answer: No, if somewhere is a iterable, instead of doing this:

temp = []
for x in somewhere:
   temp.append(x)
answer = min(temp)

you can do this:

answer = min(somewhere)

Example:

answer = min(float(line) for line in open('floats.txt'))
John Machin
  • 81,303
  • 11
  • 141
  • 189
0

Python List clear() Method
The clear() method removes all the elements from a list.

fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
print(fruits)
# outputs: []
FFish
  • 10,964
  • 34
  • 95
  • 136