0

I want to make a list of lists where each sublist will have its second value greater than the second value of its predecessor. e.g. my list_of_lists = [[1, 1], [1, 2], [1, 3], [1, 4],...]

I thought this would do it but I can't see why it is not appending the correct current version of the list.

I would like to thank Scott and gionni for the work they put in helping me to understand why my attempt was wrong and the key role object references make in this.

Code

mylist = [1,1]
mylist_of_lists = []
for i in list(range(1,11,1)):
    print("i: ", i)
    mylist[1] = i
    print("mylist : ", mylist)
    mylist_of_lists.append(mylist)
    print("mylist_of_lists : ", mylist_of_lists)
print(mylist_of_lists)

Console return

i:  1
mylist :  [1, 1]
mylist_of_lists :  [[1, 1]]
i:  2
mylist :  [1, 2]
mylist_of_lists :  [[1, 2], [1, 2]]
i:  3
mylist :  [1, 3]
mylist_of_lists :  [[1, 3], [1, 3], [1, 3]]
i:  4
mylist :  [1, 4]
mylist_of_lists :  [[1, 4], [1, 4], [1, 4], [1, 4]]
i:  5
mylist :  [1, 5]
mylist_of_lists :  [[1, 5], [1, 5], [1, 5], [1, 5], [1, 5]]
i:  6
mylist :  [1, 6]
mylist_of_lists :  [[1, 6], [1, 6], [1, 6], [1, 6], [1, 6], [1, 6]]
i:  7
mylist :  [1, 7]
mylist_of_lists :  [[1, 7], [1, 7], [1, 7], [1, 7], [1, 7], [1, 7], [1, 7]]
i:  8
mylist :  [1, 8]
mylist_of_lists :  [[1, 8], [1, 8], [1, 8], [1, 8], [1, 8], [1, 8], [1, 8], [1, 8]]
i:  9
mylist :  [1, 9]
mylist_of_lists :  [[1, 9], [1, 9], [1, 9], [1, 9], [1, 9], [1, 9], [1, 9], [1, 9], [1, 9]]
i:  10
mylist :  [1, 10]
mylist_of_lists :  [[1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10]]
[[1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10], [1, 10]]

Desired output

my list_of_lists = [[1, 1], [1, 2], [1, 3], [1, 4],...]

Windy71
  • 851
  • 1
  • 9
  • 30
  • 2
    Does this answer your question? [Why is this python generator returning the same value everytime?](https://stackoverflow.com/questions/31687185/why-is-this-python-generator-returning-the-same-value-everytime) – gionni Dec 15 '21 at 14:17
  • Hi gionni, it might do, I think I have to study yields first as I couldn't get any of the functions on that answer to do anything. – Windy71 Dec 15 '21 at 14:28
  • 1
    The generator is not important, what matters is the explanation of references – gionni Dec 15 '21 at 14:34
  • 1
    Hi gionni, so my list of lists was really something like [[ref to mylist], [ref to mylist]...] and each time mylist was being updated that update was passed to each list in the list of lists as those sublists were having the same reference updated? – Windy71 Dec 15 '21 at 14:58

1 Answers1

1

Occam's Razor: the reason they all look the same is that they all are the same.

You need to create a new list for each call to append to add (for example, append([mylist[0],i])).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Hi Scott, I don't understand how a list in my list_of_lists is getting overwritten by the next iteration of the list, or the new call you mention. Apologies, I just don't understand that. – Windy71 Dec 15 '21 at 14:17
  • 2
    It is not being *overwritten*; you are appending references to the same list (`mylist`) each time, which is why when you change it all the sublists in `mylist_of_lists` change accordingly. Instead, you need to make a new list for each call to `append`. – Scott Hunter Dec 15 '21 at 14:28
  • Hi Scott, I understand references, but (please excuse scant knowledge here) I thought my list_of_lists was being updated each time by the appending of the new list. From what I can tell I need to understand yield and generators to do this. When you say "a new list for each call to append" I am not sure what you mean, does that mean a new name for the list of lists or a new name for the my list? I'm sorry, I know how frustrating it is explaining things to some one who isn't getting it, apologies. – Windy71 Dec 15 '21 at 14:42
  • 1
    Please see edited answer. This has *nothing* to do with `yield` or generators. – Scott Hunter Dec 15 '21 at 14:44
  • Hi Scott, when I was updating mylist by mylist[1] = I did that mean that each instance of my list in the list of lists was having that update? – Windy71 Dec 15 '21 at 14:49
  • I added these two lines to the for loop to try to understand the answer (which works). print("mylist id: ", id(mylist)) print("***** ", id([mylist[0],i])) id(mylist) is the same reference each time as you said, but [mylist[0],I] had a new id each time. Is that because you were making a new list object by using [mylist[0],I]? – Windy71 Dec 15 '21 at 15:02
  • For anyone who doesn't understand the references this helped me https://programmerall.com/article/4286501147/ – Windy71 Dec 15 '21 at 15:42