0

My problem is that I need to make list of lists of specific size, like this:

list = [[]] * 10

but whe I try to append something into list inside list and print it out, this happen:

[['something'], ['something'], ['something'], ['something'], ['something'], ['something'], ['something'], ['something'], ['something'], ['something']]

And I only changed one item in list. My friend told me that there is some problem with memory adresses, but I don't know how to code it. Anyways, here is the code:

list = [[]] * 10

temp = list[3]
temp.append("something")
list[3] = temp

print(list)
  • when you create a list by doing ```l= [[]]*10```, you created a list of lists where each inner list is the SAME list. if you change l[0], you'll also be changing l[1],l[2],l[3],... appending "something" to temp sets EVERY inner list in L to include something. the last line does nothing, because it's setting an inner list to be the same list – smcrowley Jul 21 '22 at 17:16
  • if you were to ```print(list)``` after you append "something" to temp, you would see that all of the inner lists now contain "something" – smcrowley Jul 21 '22 at 17:17

0 Answers0