We can separate the output of combinations in Python in a few different ways:
from itertools import combinations
sample_list = [1, 2, 3, 4]
comb_list = list(combinations(sample_list, 2))
# Output: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
from itertools import combinations
sample_list = [1, 2, 3, 4]
comb_list = combinations(sample_list, 2)
for i in comb_list:
print(i)
# Output:
# (1, 2)
# (1, 3)
# (1, 4)
# (2, 3)
# (2, 4)
# (3, 4)
from itertools import combinations
sample_list = [1, 2, 3, 4]
comb_list = combinations(sample_list, 2)
comb_str = '\n'.join([' '.join(map(str, tpl)) for tpl in comb_list])
print(comb_str)
# Output:
# 1 2
# 1 3
# 1 4
# 2 3
# 2 4
# 3 4
Asked: 2023-03-21 11:00:00 +0000
Seen: 7 times
Last updated: Mar 15 '22