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 Conversion to string and split

Any Idea when I do the split on list( after converting to string) I am not getting the first and the last elements in the list….

if __name__ =="__main__":
    lst1= ['3 6 2 5'];
    lst1=str(lst1);
    a = [int(i) for i in lst1.split(' ') if i.isdigit()]
    print(a);

Outputs

[6, 2]

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

What I am looking for is

[2,3,5,6]

I think its due to the split characters which it finds after the 3(first element), but not sure how to resolve it.

>Solution :

Try it.

lst1= ['3 6 2 5'];
a = [int(i) for i in lst1[0].split(' ') if i.isdigit()]
a.sort()
print(a)
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