Square matrix raised to n - Assignment problem


I completed the assignment problem that was given at the end of “Lists” session. A square matrix is given and asked to find out A^n. I will come to the implementation of it later but one syntax that was different between what I did and what was shown is solution is the following

temp_prod = [item for item in sq_mat] is what I used while in the assignment there is an extra “[:]” after the first item

The syntax that I used also worked for me and did not face any issues

Just wondering if both are correct


Hi Hari,

Could you please attach some screenshots which single out your query as a reply to this thread?

What I meant is, I assign a temp list this way
temp_prod = [item for item in sq_mat]

What the solution showed was
temp_prod = [item[:] for item in sq_mat]

It was explained that the [:] was added after item as this is a 2-D list

But even without the [:], my code gave the same result. Just wondering whether I missed something



This example should explain :

list_a = [0,1,2,3,4,5,6]

slice_of_list_a_start_end_indexed = list_a[2:5]
print("slice_of_list_a_start_end_indexed: ",slice_of_list_a_start_end_indexed)

slice_of_list_a_end_indexed = list_a[:2]
print("slice_of_list_a_end_indexed: ",slice_of_list_a_end_indexed)

print("slice_of_list_a_start_indexed: ",slice_of_list_a_start_indexed)

print("slice_of_list_a_not_indexed: ",slice_of_list_a_not_indexed)


slice_of_list_a_start_end_indexed:  [2, 3, 4]
slice_of_list_a_end_indexed:  [0, 1]
slice_of_list_a_start_indexed:  [4, 5, 6]
slice_of_list_a_not_indexed:  [0, 1, 2, 3, 4, 5, 6]

Check the difference between each slice of the list.
The names of the slices are self explanatory. (I hope).

Hope this explains.

Hi @hari.yajurveda,

The reason they are giving the same output is because they are IMPLICIT and EXPLICIT methods. items[:] is explicit way, where all the elements in sub-list (all elements in a row in your sq_mat) will be assigned to item[:] in each iteration. Mr Pandurang’s answer will complement this.

Your method is implicit where for each iteration in ‘for’ loop, sub-list (rows) as a whole will be assigned to the variable item. Both are correct

Thank you very much. This explains it