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
>>
>> 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.
_
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
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 :
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']
[