(Nov-12-2020, 05:12 PM)giorgosmarga Wrote: Because i am confused shouldnt that code return the emptylist with 8 shuffled differenlty sublists?
No, it shouldn't. There is only one list and you shuffle it 8 times.
Maybe following code can explain:
>>> nums = [1, 2, 3] >>> emptylist = [] >>> for i in range(3): ... emptylist.append(nums) # you are adding same list n times ... >>> emptylist [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> for item in emptylist: ... print(id(item)) ... 140256603923232 # same object 3 times 140256603923232 # or more correctly three references to same object 140256603923232 >>> emptylist[0][0] = 'no way' # changing item in emptylist >>> emptylist # changes all items because they reference same object [['no way', 2, 3], ['no way', 2, 3], ['no way', 2, 3]] >>> nums[1] = "it's happening" # it's getting worse; you change original list >>> emptylist # and as this is same object it is reflected in emptylist [['no way', "it's happening", 3], ['no way', "it's happening", 3], ['no way', "it's happening", 3]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.