I would expect the output to be like [[0,0,0,0,],[0,1,0,0],[0,2,0,0],[0,3,0,0]]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



I would expect the output to be like [[0,0,0,0,],[0,1,0,0],[0,2,0,0],[0,3,0,0]]


time_count = [[0, 0, 0, 0]] * 4
j = 0
for i in range(len(time_count)):
time_count[i][1] = j
j += 1
print(time_count)



Output:


[[0, 3, 0, 0], [0, 3, 0, 0], [0, 3, 0, 0], [0, 3, 0, 0]]



I would expect the output to be like:


[[0,0,0,0],[0,1,0,0],[0,2,0,0],[0,3,0,0]]



can someone explain why every index[1] is 3 ?


index[1]


3





The * operator creates new references to the (same) list. If you change one you are changing all.
– Klaus D.
Aug 8 at 7:42


*





Bravo!! I see but How can I create a long list without writing every element down?
– bitbitbitter
Aug 8 at 7:50




3 Answers
3



Easy fix:


time_count = [[0, 0, 0, 0] for _ in range(4)]



As Klaus D. has alluded, using the * operator on nested lists is usually a bad idea, as it duplicates the contents by copying the references. You won't notice this when multiplying sequences of immutable types, e.g. a list of integers, but when the elements are mutable, you get duplicates.


*



The * operator just creates a new reference to the original list (here [0, 0, 0, 0]).


[0, 0, 0, 0]



If you change the original list as time_count[i][1] = j, all the references reflect the same changes.


time_count[i][1] = j



If you want to create a new list, you can use the extend method with a loop on it :


time_count =
time_count.extend([0,0,0,0] for _ in range(4))



Now, all the element list in time_count store separate memory and can have different values.



Do:


time_count = [[0, 0, 0, 0]] * 4
print([[v if x!=1 else i for x,v in enumerate(a)] for i,a in enumerate(time_count)])



Output:


[[0, 0, 0, 0], [0, 1, 0, 0], [0, 2, 0, 0], [0, 3, 0, 0]]



Explanation:



Use enumerate to iterate trough both the indexes and values of the list time_count.


enumerate


time_count



Use enumerate again to iterate trough both the indexes and values of the list a. which is the iterator for time_count.


enumerate


a


time_count



Go trough the indexes of a and say i (the index iterator for time_count) if x (the index iterator for a) is 1, otherwise say v (the iterator for a)


a


i


time_count


x


a


v


a



Note: This is all in a list comprehension





Bro! can you break down the above print statement in plain English? What exactly does it mean??
– bitbitbitter
Aug 9 at 9:43





@bitbitbitter Edited my answer
– U9-Forward
Aug 9 at 10:52






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard