Python Practice › List Programs › Program 49
Positives left, negatives right
Write a Python program to move all positive numbers to the left and negative numbers to the right, keeping their order.
Input
[2, -4, 6, 44, -7, 8, -1, -10]
Output
[2, 6, 44, 8, -4, -7, -1, -10]
Approach
- Join a list of non-negative values with a list of negative values.
Solution
numbers = [2, -4, 6, 44, -7, 8, -1, -10]print([n for n in numbers if n >= 0] + [n for n in numbers if n < 0])Output
[2, 6, 44, 8, -4, -7, -1, -10]
Learn the concept
New to this topic? Read the lesson, then come back.