Quantcast
Viewing all articles
Browse latest Browse all 12

Answer by boardrider for How do I sort a list with positives coming before negatives with values sorted respectively?

I don't know if it's the most Pythonic, and it certainly does not have any bells-and-whistles, but IMO it's a clear and understandable code:

lst = [1, -2, 10, -12, -4, -5, 9, 2]pos = list()neg = list()for i in lst:    neg.append(i) if i < 0 else pos.append(i)print(sorted(pos) + sorted(neg))

Viewing all articles
Browse latest Browse all 12

Trending Articles