Program to Move all Negative Elements to End in Order with Extra Space Allowed

Python Program to Move all Negative Elements to End in Order with Extra Space Allowed

In the previous article, we have discussed Python Program to Split the Array/List and add the First Part to the End
Given a list that contains the negative and positive elements the task is to move all the negative elements to the end of the list.

Examples:

Example1:

Input:

Given List =[1, 9, -3, 6, 8, 11, 35, -5, -7, 10, -1, -2, -3]

Output:

The given list after moving negative elements to the end is [1, 9, 6, 8, 11, 35, 10, -3, -5, -7, -1, -2, -3]

Example2:

Input:

Given List = [-7 11 56 -3 -8 12 17 -25 -8 -11 -6 9 10]

Output:

The given list after moving negative elements to the end is [11, 56, 12, 17, 9, 10, -7, -3, -8, -25, -8, -11, -6]

Program to Move all Negative Elements to End in Order with Extra Space Allowed in Python

There are several ways to move all the negative elements to the end of the list some of them are:

Method #1: Using List Comprehension (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Using list comprehension and if conditional statements separate the positive numbers from the list and store it in a variable to say the positive list.
  • Using list comprehension and if conditional statements separate the negative numbers from the list and store it in a variable to say the negative list.
  • Add the positive list and negative list using the + operator and store it in another variable to say result list(This operation moves all the negative list).
  • Print the result list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlst = [1, 9, -3, 6, 8, 11, 35, -5, -7, 10, -1, -2, -3]
# Using list comprehension and if conditional statements
# separate the positive numbers from the list
# and store it in a variable to say the positive list.
pstivelist = [elemn for elemn in gvnlst if elemn >= 0]
# Using list comprehension and if conditional statements
# separate the negative numbers from the list and store it
# in a variable to say the negative list.
ngtivelist = [elemn for elemn in gvnlst if elemn < 0]
# Add the positive list and negative list using the + operator
# and store it in another variable to say result list
# (This operation moves all the negative list).
resltlist = pstivelist+ngtivelist
# Print the result list.
print('The given list after moving negative elements to the end is', resltlist)

Output:

The given list after moving negative elements to the end is [1, 9, 6, 8, 11, 35, 10, -3, -5, -7, -1, -2, -3]

Method #2: Using List Comprehension (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Using list comprehension and if conditional statements separate the positive numbers from the list and store it in a variable to say the positive list.
  • Using list comprehension and if conditional statements separate the negative numbers from the list and store it in a variable to say the negative list.
  • Add the positive list and negative list using the + operator and store it in another variable to say result list(This operation moves all the negative list).
  • Print the result list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlstt = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Using list comprehension and if conditional statements
# separate the positive numbers from the list
# and store it in a variable to say the positive list.
pstivelist = [elemn for elemn in gvnlstt if elemn >= 0]
# Using list comprehension and if conditional statements
# separate the negative numbers from the list and store it
# in a variable to say the negative list.
ngtivelist = [elemn for elemn in gvnlstt if elemn < 0]
# Add the positive list and negative list using the + operator
# and store it in another variable to say result list
# (This operation moves all the negative list).
resltlist = pstivelist+ngtivelist
# Print the result list.
print('The given list after moving negative elements to the end is', resltlist)

Output:

Enter some random List Elements separated by spaces = -7 11 56 -3 -8 12 17 -25 -8 -11 -6 9 10
The given list after moving negative elements to the end is [11, 56, 12, 17, 9, 10, -7, -3, -8, -25, -8, -11, -6]

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.