Doubt in Week 5 (Python Continued...) Solution for List's Problem [7:56]

Hi Team,
Just confused about one thing.

I couldn’t find any difference between 1 and 2 -> a being the list [[1,1], [1,0]]
1/ output = [i[:] for i in a]
2/ output = [i for i in a]

When i print the result its the same.

But to my surprise, this isn’t seem to work when for matrix multiplication.

But as soon as I revert the code, output = [i[:] for i in a]
this seems to work fine?
Can anybody help?

All screen shots attached.
Thanks in Advance! :slight_smile:

b = [i[:] for i in a]. <-- This will create a new copy, b. Further changes in a or b will only effect respective list
c = [i for i in a[ <-- Here elements in c point to elements in a. If you change a or c, it will effect the other.

In your case, you need new copy to store your multiplication results.

1 Like