Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

split 3 values seperated by commas into 3 different pairs

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading