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

Python List content stripping

So, I have a list having string such as

matches=['A vs B','C vs D','E vs F','G vs H']

I want to get strip the list contents in such a way that I get two more lists of

t1=[A,C,E,G]
t2=[B,D,F,H]

I’ve tried strip function as

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

t1=[i.strip("vs")[0] for i in matches]
t2=[i.strip("vs")[-1] for i in matches]

But I’m not getting the desirable results. Can someone guide me trough it. Thanks

>Solution :

I would use :

t1, t2 = map(list, zip(*[m.split(" vs ") for m in matches]))

And since you have a tag, here is any approach with split and the Series constructor :

t1, t2 = (
            pd.Series(matches)
                .str.split(" vs ", expand=True)
                .values
                .T.tolist()
          )

Output :

print(t1)
#['A', 'C', 'E', 'G']
print(t2)
#['B', 'D', 'F', 'H']
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