# List Comprehensions Problem

You are given three integers x,y,z and representing the dimensions of a cuboid along with an integer n . You have to print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i+j+k is not equal to n Here,
0<= i <= x
0<= j <= y
0<= k <= z

My Code:

``````if __name__ == '__main__':
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:
list_1 = [i,j,k]
list_2 = [list_1]
print(f"{list_2}", end='')
``````

My Output (stdout):
[[0, 0, 0]],[[0, 0, 1]],[[0, 1, 0]],[[1, 0, 0]],[[1, 1, 1]],

Expected Output:
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

How to deal with that bracket, my logic is correct but just a bit doubt here why my answer is not matching to the expected output.

You are creating and printing a second list each time you reach the innermost for-loop.
If you want a single list that contains all results, you have to do something like this:

``````results = []
for ...
for ...
for ...
if ...
results.append((i,j,k))
print(results)``````
1 Like

Nope, it gives me an error that
Traceback (most recent call last):
File “Solution.py”, line 11, in
list_2.append(i,j,k)
TypeError: append() takes exactly one argument (3 given)

So I can’t append more than one argument ¯_(ツ)_/¯

Kindly reread that line in my example. 1 Like

¯(°_o)/¯ Ok got it, I didn’t saw that bracket , Thanks

Just a quick look but I think this line is the problem. You are making a list, but I think list_1 is already a list, so you are wrapping a list with a list. I think if you just get rid of this list?

I think that’ll fix it, or you have something indented wrong. Any time you get nested forloops you should use variables like “row”, “column”, “cell”, etc. instead of i, j, k. It’ll help you know where you should do what.

1 Like