Change result of permutations from tuple to list

I am trying to create a list of list containing all possible permutations of int 1~6. What I hope to get is [[1,2,3,4,5,6], [1,2,3,4,6,5]…..] but keep getting tuples. Is there any elegant way for doing following code?

n=6
numberPool = []
for i in range(1, n+1):
    numberPool.append(i)
nutPool = list(permutations(numberPool))
# change to list from tuple
temp = []
for i in nutPool:
    temp.append(list(i))
nutPool = temp

Answer

Simply, you can use a comprehension to change each tuple to a list:

nutPool = [list(perm) for perm in permutations(numberPool)]

which is essentially condensing these five lines in your question into a single expression:

nutPool = list(permutations(numberPool))
temp = []
for i in nutPool:
    temp.append(list(i))
nutPool = temp

Leave a Reply

Your email address will not be published. Required fields are marked *