How can I use a for loop to strip parens from list elements and print them one at a time?

27 Views Asked by At

I have a list from an n_choose_k operation. The list is composed of number combinations that are each surrounded by parentheses.

import itertools

number = 9
k = 3
n = range(1,number)
combos = list(itertools.combinations(n,k))
print(combos)

[(1,2,3), (1,2,4), (1,2,5), . . . ]

I want to strip the parentheses from each combination in the list, and print them out one at a time, like this:

1,2,3
1,2,4
1,2,5
etc.

I know how to strip the parens from each individual list element:

print(str(combos[0]).strip('()')

1,2,3

But if I try to do it for all elements for a for loop, I get a TypeError:

for i in combos:
    print(str(combos[i]).strip('()')

The TypeError says that I must use indices or slices instead of tuples. I'm not sure what the error is getting at. How can use the for loop to strip the parens from each combination element and print those elements one at a time?

0

There are 0 best solutions below