47

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.

5 Answers 5

60

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
i see. that helps @david: yah, there's not much difference for me. but my newbness has gotten lessened :P
"arrays are different": not really, del works; see my answer
@John, I didn't mean del wouldn't work, just that lists and arrays are different.
34

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.

4 Comments

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.
I prefer this approach as coming from a VBA background we use array = ("") so very similar and easy to rememeber! +1
This does not quite work. Consider the following code. ``` a = [1,2,3] b = a ``` After this we have two senecios. 1. a = [] # b == [1,2,3] 2. a.clear() # b == []
@tcglezen That's using a different definition of "work" that was not part of the question.
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]

2 Comments

Far too few answers know the difference between a python array type and a python list. +1 for you.
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
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'))

Comments

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: []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.