PDA

View Full Version : How to empty an array


d.i.z
2004-12-17, 01:44
how could i empty the array which is already filled with data?

i tried everything, splice, clear, assigning some empty data and still nothing.

the array is defined globally, for example myarray = []

and then filled with data with myarray.append('something')

d.i.z
2004-12-17, 02:55
ok, now i know:
for i in range(0, len(myarray)):
myarray.pop()

i 'borrowed' it from gametrailers script :)

and sorry for posting in wrong forum (now corrected).

Asteron
2004-12-17, 05:44
how could i empty the array which is already filled with data?

i tried everything, splice, clear, assigning some empty data and still nothing.

the array is defined globally, for example myarray = []

and then filled with data with myarray.append('something')
i think
myarray[:] = []

which is equivalent to

myarray[0:len(myarray) - 1] = []

explicitly deletes every element.

Kaer
2004-12-17, 13:43
ok, now i know:
for i in range(0, len(myarray)):
myarray.pop()

i 'borrowed' it from gametrailers script :)

and sorry for posting in wrong forum (now corrected).
haha im the gametrailers author and was abt to post the same thing. i cant find a better method to delete though, will try asteron method nxt time

solexalex
2004-12-17, 16:17
what about
myarray=[] ?

or

del myarray

Asteron
2004-12-17, 17:17
i think that those two dont free up what the list refers to


from http://rgruet.free.fr/pqr2.3.html#list

s.pop() is the same as x = s[-1]; del s[-1]; return x
where s[-1] = s[len(s)-1]

also s[:] = [] is the same as s[0:len(s)-1] = []
which is the same as del s[0:len(s)-1]

i really would wish there was a nice clear() though.

d.i.z
2004-12-19, 15:42
forgot to say thanks ;)

thanks asteron, it works well and seems to be clearer than kaer's solution (but thanks kaer too for the initial idea :))

and here is my small script: http://prgk.xtina.pl/xbmc/gamescommercials.py
this is for browsing majkel's game commercials page (http://www.majkel.mds.pl/html/reklama/)

as soon as i implement few more ideas i still have left, i will upload it to xbmc scripts page.