0

It might be idiotic but drives me crazy: Why this code doesn't work? Gives me a warning of “Redeclared j variable defined above without usage” and doesn't rewrites elements. Thanks.

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

for i in a:
    for j in i:
        j=0

print(a)
barry23
  • 9
  • 2

1 Answers1

0

The warning message is trying to tell you just what it says: You are technically "redeclaring" a variable. (Though it's better thought of as a name, to which you are re-assigning.)

Specifically, a for-loop is basically an implicit re-assignment of the name j to the next int in i. Your IDE detects that you don't do anything with that temporary assignment before doing a blanket-j=0 within each nested for-loop.

You have probably noticed that all of this does not actually modify a. That's because, as pointed out in the comments, you're not mutating the actual elements (or sub-elements) of a.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235