Let me first describe the question on which I am working. Its a question from hackerrank - List Comprehensions.
Well here's my solution to that question :
final_list = [];
temp_list = [];
x = int(input());
y = int(input());
z = int(input());
n = int(input());
for i in range(x+1):
for j in range(y+1):
for k in range(z+1):
if((i + j + k) != n):
temp_list.clear();
temp_list.append(i);
temp_list.append(j);
temp_list.append(k);
final_list.append(temp_list);
print(final_list);
I used these values as my input : x = 1, y = 1 , z = 1 and n = 2.
Using these values I got output : [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
I am not getting that even I cleared the temp_list then why am I getting this output instead of : [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]].
and moreover, when I declared the temp_list in the if condition itself instead of declaring on the top of the code, I got the answer. can anyone let me know why is this happening ?