Clearing an array

Jason Tackaberry tack at urandom.ca
Wed Jul 29 11:39:00 EDT 2009


On Wed, 2009-07-29 at 08:24 -0700, WilsonOfCanada wrote:
> 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.




More information about the Python-list mailing list