Clearing an array

W

WilsonOfCanada

Hellos,

I was wondering if there is any built-in function that clears the
array. I was also wondering if this works:

arrMoo = ['33', '342', .... '342']
arrMoo = []
 
D

Diez B. Roggisch

WilsonOfCanada said:
Hellos,

I was wondering if there is any built-in function that clears the
array. I was also wondering if this works:

arrMoo = ['33', '342', .... '342']
arrMoo = []

Most of the time, yes. Unless you have something like this, then it won't
work:


foo = [10]

bar = foo # an alias

foo = []

assert foo == bar

Then what you need to to is this:

foo[:] = []


Diez
 
J

Jason Tackaberry

I was wondering if there is any built-in function that clears the
array.

The proper python term would be "list." You can remove all elements of
a list 'l' like so:

del l[:]

I was also wondering if this works:

arrMoo = ['33', '342', .... '342']
arrMoo = []

That doesn't clear the list as such, but rather creates a new list, and
reassigns the new list to the 'arrMoo' name in the local scope.
Consider:
>>> l1 = [1,2,3]
>>> l2 = l1
>>> l1 = []
>>> print l2
[1, 2, 3]

So the original list 'l1' lives on. However:
>>> l1 = [1,2,3]
>>> l2 = l1
>>> del l1[:]
>>> print l1, l2
[] []

Cheers,
Jason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top