49. Problem to move all positive numbers on the left

In this Python list program, we will take a user input as a list and move all positive numbers to the left side and negative numbers to the right side with the help of the below-given steps.

Move all positive numbers in a list:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable var and assign its value equal to 0.
  3. Use for loop with the range function to iterate over elements in the list.
  4. If the iterated element is greater than 0 then assign its value to the temp variable.
  5. Assign the value of that index number equal to list1[var]
  6. Now change the value of list1[var] to temp variable.
  7. Add 1 to the var variable.
  8. Print the output to see the result i.e. move all positive numbers to the left.
				
					#Input list
list1 = [2, -4, 6, 44, -7, 8, -1, -10]
var = 0

#Sorting list
for i in range(0,len(list1)) :
    if (list1[i] > 0) :
        temp = list1[i]
        list1[i] = list1[var]
        list1[var]= temp
        var += 1

#Printing output        
print(list1)
				
			

Output :

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

Related Articles

Iterate over lists and create a list of sublists

Move all zero digits to the end

Leave a Comment