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

How can i automate this process?
I've tried list comprehension and for loop and while loop for few hours but couldn't figure it out

  • There are 28 pieces
  • No duplicates i.e [1, 2] == [2, 1]
Mitra
  • 13
  • 6

2 Answers2

2

You can base the inner loop starting index on the outer loop

dominoes = []
    for i in range(7):
        for j in range(i, 7):
            dominoes.append([i, j])

Or with list comprehensions

dominoes = [[i, j] for i in range(7) for j in range(i, 7)]
# output: [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 3], [3, 4], [3, 5], [3, 6], [4, 4], [4, 5], [4, 6], [5, 5], [5, 6], [6, 6]]
Guy
  • 46,488
  • 10
  • 44
  • 88
0

If you want a non-duplicating data structure, you can use the set(), and as your data won't be modifiable, store it as a tuple rather than a list.

To prevent duplication, you can add a condition that each tuple must follow a format of (i, j) where i <= j.

dominoes = set()

for i in range(7):
    for j in range(7):
        if i <= j:
            dominoes.add((i, j))

print(dominoes)