using Iterators to get a particular sequence of list from [1, 2, 3]

Question:

I want to achieve this output

[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

from this given list: [1,2,3].

I have tried using

foo = [1, 2, 2]
print(list(permutations(foo, 3)))

and this

l

ist(it.combinations_with_replacement([1,2,3], 3))

from itertools import combinations 
  
def rSubset(arr, r): 
   
    return list(combinations(arr, r)) 
  
 
if __name__ == "__main__": 
    arr = [1, 2, 3] 
    r = 3
    print (rSubset(arr, r))

but am not able to get the required result any help please

Asked By: Azucode

||

Answers:

You want a product, not a permutation or combination.

from itertools import product

print(list(product([1,2,3], repeat=3)))
Answered By: chepner