-6

I want to create a list of lists using list comprehension. ex [range(3),range(3),range(3)] i.e all possible coordinates of a 3d system from 0 to 3 How to do this using only list comprehension and nothing else

I tried doing

coordinates=[x for x in range(3)[y for y in range(3)[z for z in range(3)]]]
dot.Py
  • 5,007
  • 5
  • 31
  • 52
Rabbani
  • 29
  • 1
  • 4
  • 3
    What have you tried so far? Show your current code and where you're having the problem, and then state what the problem is. – Matt Hogan-Jones Mar 29 '17 at 11:43
  • 1
    Possible duplicate of [Combination of 1 and 0 in an array in Python](http://stackoverflow.com/questions/24609919/combination-of-1-and-0-in-an-array-in-python) – dot.Py Mar 29 '17 at 11:43

2 Answers2

4

your syntax is slightly off

coordinates=[[x,y,z] for x in range(4) for y in range(4) for z in range(4)]
jfs
  • 399,953
  • 195
  • 994
  • 1,670
R-4
  • 75
  • 4
2
[[n,d,e] for e in [0,1,1] for d in [0,0,0] for n in [1,1,1]]

Output:

[[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
<..>

Change/add variables and source lists as needed.

danny
  • 5,140
  • 1
  • 19
  • 31