in my python code i want to create a list of lists…in a loop …
but after completion of the loop i end up with the same list in all elements…is it because the lists are pointed to and not stored ? If so, how can i come up with a solution to my problem ?
Following is my code
list_lists=list()
list_temp=list()
for i in xrange(n):
ind_count =0
del list_temp[0:len(list_temp)]
for j in xrange(no_words):
if inp[i] == words[j]:
list_temp.append(j)
list_lists.append(list_temp)
Instead of deleting the items of your list (
del list_temp[0:len(list_temp)]) just assign a new list:list_temp = list(). You can also use the shorthand:list_temp = []The problem that you are facing is that you are always reusing the same list you created in the second line:
list_temp = list(). You are just inserting references to this one list over and over again.