Search code examples
pythonlistarraylistpython-itertools

How can I change the first element of a list?


I have this list of elements:

[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2]]

and I would like to increase it like this:

[[1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0], [1, 1, 1].................] then with 2:

[[2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1].................] and so on until 5.

I dont want to hard code so I was trying itertools but I could only reach up to 2 like this:

first2 = list(map(list, itertools.product(list(range(3)), repeat = 3)))

Any help is highly appreciated.


Solution

  • Use itertools.product() to create the 2nd and 3rd elements, and an outer loop to prepend 0 through 5 to them.

    result = []
    for i in range(6):
        result.extend([i] + list(x) for x in itertools.product(range(3), repeat = 2))