Python Program to Put Positive and Negative Numbers in Separate List

Write a Python Program to Put Positive and Negative Numbers in Separate List using For Loop, While Loop and Functions with a practical example.

Python Program to Put Positive and Negative Numbers in Separate List using For Loop

In this python program, we are using For Loop to iterate every element in a given List. Inside the Python loop, we are using the If statement to check whether the list item is Positive or Negative. Based on the result, we are appending that item to the Positive list or the Negative list.

# Python Program to Put Positive and Negative Numbers in Separate List

NumList = []
Positive = []
Negative = []

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

for j in range(Number):
    if(NumList[j] >= 0):
        Positive.append(NumList[j])
    else:
        Negative.append(NumList[j])

print("Element in Positive List is : ", Positive)
print("Element in Negative List is : ", Negative)
Python Program to Put Positive and Negative Numbers in Separate List 1

In this python program, the User entered List items = [12, -34, 55, -87, 67]

For Loop – First Iteration: for 0 in range(0, 5). The condition is True. So, it enters into the If Statement
if(NumList[0] >= 0) => if(12 >= 0) – Condition is True
Positive.append(NumList[0]) => Positive = [12]

Second Iteration:for 1 in range(0, 5) – Condition is True
if(NumList[1] >= 0) => if(-34 >= 0) – Condition is False. So, it enters into the Else block.
Negative.append(NumList[1]) => Negative = [-34]

Third Iteration:for 2 in range(0, 5) – Condition is True
if(NumList[2] >= 0) => if(55 >= 0) – Condition is True
Positive.append(55) => Positive = [12, 55]

Fourth Iteration:for 3 in range(0, 5) – Condition is True
if(-87 >= 0) – Condition is False and it enters into Else block.
Negative.append(-87) => Negative = [-34, -87]

Fifth Iteration:for 4 in range(0, 5) – Condition is True
if(67 >= 0) – Condition is True
Positive.append(67) => Positive = [12, 55, 67]

Sixth Iteration:for 5 in range(5) – Condition is False. So it exits from Python For Loop

Python Program to Put Positive and Negative Numbers in Separate List using While loop

This Python program to place positive numbers in Positive List and negative numbers in Negative List is the same as the above. We just replaced the For Loop with While loop.

# Python Program to Put Positive and Negative Numbers in Separate List

NumList = []
Positive = []
Negative = []
j = 0

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

while(j < Number):
    if(NumList[j] >= 0):
        Positive.append(NumList[j])
    else:
        Negative.append(NumList[j])
    j = j + 1

print("Element in Positive List is : ", Positive)
print("Element in Negative List is : ", Negative)

Positive and Negative Numbers in Separate List using a while loop output

Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 2
Please enter the Value of 2 Element : -3
Please enter the Value of 3 Element : -5
Please enter the Value of 4 Element : 9
Please enter the Value of 5 Element : -8
Please enter the Value of 6 Element : 7
Element in Positive List is :  [2, 9, 7]
Element in Negative List is :  [-3, -5, -8]

Python Program to Put Positive and Negative Numbers in Separate List using Functions

This Python separate positive and negative list numbers example is the same as the first example. However, we separated the logic using Functions. Remember, instead of writing separate functions for Positive and Negative, you can also write a single function.

# Python Program to Put Positive and Negative Numbers in Separate List
def positive_numbers(NumList):
    Positive = []
    for j in range(Number):
        if(NumList[j] >= 0):
            Positive.append(NumList[j])
    print("Element in Positive List is : ", Positive)

def negative_numbers(NumList):
    Negative = []
    for j in range(Number):
        if(NumList[j] < 0):
            Negative.append(NumList[j])
    print("Element in Negative List is : ", Negative)
    
NumList = []
Positive = []
Negative = []
j = 0
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

positive_numbers(NumList)
negative_numbers(NumList)

Positive and Negative Numbers in Separate List output

Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : -23
Please enter the Value of 3 Element : -44
Please enter the Value of 4 Element : 67
Please enter the Value of 5 Element : -98
Please enter the Value of 6 Element : -3
Element in Positive List is :  [12, 67]
Element in Negative List is :  [-23, -44, -98, -3]