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

Sort a list by negative numbers, zeros, then positive Python

How do I make a new list where the numbers are sorted first in negative, then 0’s and then positive numbers in Python?

For example if I have the list a = [3, -5, 1, 0, -1, 0, -2]
I want the new list to be [-5, -1, -2, 0, 0, 3, 1]

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

>Solution :

You could do something like that

b = [x for x in a if x < 0] + [x for x in a if x == 0] + [x for x in a if x > 0]
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