1
x = raw_input("")
y = raw_input("")
a = []  
b = []
count = 1

for i in range(0, int(y)):
    b.append(count)
    count+=1

for i in range(0, int(x)):
    a.append(b)

for i in a:
    print ""
    for j in i:
        print j, 

a[1][1] = 0



for i in a:
    print ""
    for j in i:
        print j, 

a has been created by appending list "b" n time to it Now when i modify a[1][1] the whole column that is a[0][1] - a[n][1] gets modified to that value

Can anyone explain why this is happening

0

2 Answers 2

1

Every time you append b, you are appending the same list -- not copies of the list, but multiple references to the same object. If you want each row to be a different list, you need to append a new list each time, by doing a.append(b[:]).

Sign up to request clarification or add additional context in comments.

Comments

1

That is because by appending b, you are creating pointers to the same objects. Instead, make a copy as follows:

for i in range(0, int(x)):
    a.append(b[:])

You can see it working as expected, here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.