Reputation: 3590
Ok this is my problem. I am trying something like this:
for i in big_list:
del glist[:]
for j in range(0:val)
glist.append(blah[j])
The idea is to reset the list and reuse it for the next set of data points. The problem is, for some reason, if the first list has 3 points,
glist[0]
glist[1]
glist[2]
The next list will continue from index 3 and store the last 3 elements in those indexes
glist[0] = 4th elem of new list
glist[1] = 5th elem of new list
glist[2] = 6th elem of new list
glist[3] = 1st elem of new list
glist[4] = 2nd elem of new list
glist[5] = 3rd elem of new list
I'm sure it is an issue with allocated space. But how can I achieve this del g_list[:] so the result is,
glist[0] = 1st elem of new list
glist[1] = 2nd elem of new list
glist[2] = 3rd elem of new list
glist[3] = 4th elem of new list
glist[4] = 5th elem of new list
glist[5] = 6th elem of new list
Allocating variable from within loop is not an option. Any ideas?
Upvotes: 0
Views: 647
Reputation: 76842
del glist[:]
works fine for clearing a list. You need to show us your exact code. As shown below, the behavior you're describing does not happen. The append
after the del a[:]
puts the item at index 0.
>>> a = [1,2,3]
>>> del a[:]
>>> a
[]
>>> a.append(4)
>>> a
[4]
Upvotes: 1
Reputation: 172339
Change del glist[:]
to glist = []
. You don't need to "reuse" or "reallocate" in Python, the garbagecollector will take care of that for you.
Also, you use 'i' as the loop variable in both loops. That's going to confuse you sooner or later. :)
Upvotes: 5