I’d like to split 3 values separated by commas
a = 'a,b,c'
into 3 different pairs:
(a,b), (b,c), (a,c)
How would I be able to do this on Python?
I have tried
n = [(x) for x in a.split(',')]
x = list(zip(n[1::], n[0::2]))
I get one pair: ('a', 'b')
>Solution :
I am not sure what the logic is behind grouping these pairs together:
(a,b), (b,c), (a,c)
However, if you wanted to group each element with every other element you can use this:
a = 'a,b,c'
my_list = a.split(',')
new_list = []
for i in my_list:
for j in my_list:
if i != j:
new_list.append((i,j))
And you will get
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]