Python Practice › List Programs › Program 49

Positives left, negatives right

EasyList Programs · 49 of 100

Write a Python program to move all positive numbers to the left and negative numbers to the right, keeping their order.

Advertisement
Input
[2, -4, 6, 44, -7, 8, -1, -10]
Output
[2, 6, 44, 8, -4, -7, -1, -10]

Approach

  1. Join a list of non-negative values with a list of negative values.

Solution

Python
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.

More list programs

← All 100 List Programs

Leave a Comment