Re: [Tutor] reg. list update

2017-04-17 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
This is an aliasing problem. Change the code to super = [] sub = [""]*3 other = ["a","b","c","d"] sub[0] = "hi" sub[1] = "hello" for item in other: l = sub[:] l[2] = item super.append(l) for item in super: print item regards, Sarma. On Tue, Apr 18, 2017 at 2:16 AM, Mats Wichmann

Re: [Tutor] reg. list update

2017-04-17 Thread Mats Wichmann
>> >> Is there anything wrong in this code or any feature of python? > > yeah, feature of Python. you could google for "deep copy". > the reference issue is involved here, but my explanation was off, I confused myself, listen to Peter instead :) It's just the same list four times. _

Re: [Tutor] reg. list update

2017-04-17 Thread Mats Wichmann
On 04/17/2017 12:41 PM, Rasika Sapate via Tutor wrote: > Dear Python group, > I had written following code. > > super = [] > sub = [""]*3 > other = ["a","b","c","d"] > sub[0] = "hi" > sub[1] = "hello" > for item in other: > sub[2] = item > super.append(sub) > for item in super: > print

Re: [Tutor] reg. list update

2017-04-17 Thread Peter Otten
Rasika Sapate via Tutor wrote: > Dear Python group, > I had written following code. > > super = [] > sub = [""]*3 > other = ["a","b","c","d"] > sub[0] = "hi" > sub[1] = "hello" > for item in other: > sub[2] = item > super.append(sub) > for item in super: > print item > > > Output :

[Tutor] reg. list update

2017-04-17 Thread Rasika Sapate via Tutor
Dear Python group, I had written following code. super = [] sub = [""]*3 other = ["a","b","c","d"] sub[0] = "hi" sub[1] = "hello" for item in other: sub[2] = item super.append(sub) for item in super: print item Output : ['hi', 'hello', 'd'] ['hi', 'hello', 'd'] ['hi', 'hello', 'd'] [